PDA

View Full Version : Code producers!


X-Gote
07-26-2002, 07:24 PM
Acid1 is in the club! Well I started writing a cool function at work to handle the boring task of updating record sets in a database. I found out though a pear class has already beeen written to handle such a task:
http://cvs.php.net/co.php/pear/HTML_QuickForm/QuickForm.php?r=1.33&Horde=860983a29e545f81a62f6d8668579911

I have to flex my geek ego (and then soon to follow by acid1 about how php isn't a real language!).

Update your data or insert BIATCH!


/** build some SQL. NOTE: updates/delete depend on index column named "ID" **/
function build_query($arr_obj, $table, $type) {
global $dbh; // <-- this is your DSN object.

/* error reporting. Might switch to trigger_error */
$errors = "";
if (!is_array($arr_obj)) {
$errors .= "Object is not array.<br>";
} elseif ($arr_obj == array()) {
$errors .= "Array object is empty.<br>";
}

if (!isset($type)) {
$errors .= "Type was not specified.<br>";
$type = "";
}

if ($errors != "") {
die($errors);
}

/* initialize our variables like good elite people do! */
$columns = "";
$data = "";

switch($type) {
/* used for inserting stuff in a database */
case "insert":
foreach ($arr_obj as $key => $value) {
$columns .= "`$key`,";
$data .= $cdsdbh->quote($value).",";
}
$columns = substr($columns, 0, -1);
$data = substr($data, 0, -1);

/* and now ladies, what you've been waiting for: THE QUERY! */
$sql = "INSERT INTO $table ($columns) VALUES ($data)";
return $sql;

break;

/* used for updating stuff in a database*/
case "update":
$update = "";
foreach ($arr_obj as $key => $value) {
/* filter out our crap */
if ($key == "ID" || $key == "submit") {
continue;
}

$update .= "$key=".$cdsdbh->quote($value).",\n";
}
$update = substr($update, 0, -2);

/* and now ladies, what you've been waiting for: THE QUERY! */
$sql = "UPDATE $table SET \n$update\n WHERE $table.ID = '{$arr_obj['ID']}'";
return $sql;

break;

case "delete":
return "I will write this on monday..";
break;
}
}


And why does a manager at Mc Donalds make more then I do?
http://www.rehobothtoday.com/McDonalds/images/1863small.jpg
Grin it up fatty cakes. When armageddon hits and the line is drawn to cull off of mcdonalds managers and ask php programmers to help save the human race.. it will be a sweet day indeed!

acid1
07-27-2002, 05:10 PM
code producer? i'm not a code producer, I'm a code breaker? but not the good ones that work for the govt, the bad ones the try to be code producers but break the code unintentionally

acid1