Click to See Complete Forum and Search --> : Feedback on my code


mrgrammar
12-03-2004, 09:39 PM
I have a multipart form (6 pages - I shorted the code below). Instead of writing the query over and over again for each part, I came up with a way to minimize the coding. I'd like your feedback and suggestions for improving or simplifying the code.

<?PHP

if (isset($_POST[now])) {$now == $_POST[now];}

if (isset($now)){

$fieldname = array();
$fc = 0;
$bdonorid = $_POST[donorid];

if ($now == "start"){
// PUT IN THE INFO FROM START
$insdon = "INSERT INTO donors (donorid) VALUES (".$bdonorid.")";
mysql_db_query($dbname, $insdon) or die("Error: ".mysql_error());

}elseif ($now == "1"){
// PUT IN THE INFO FROM STEP 1
$fieldname[$fc] = "husband_name"; $fc = $fc + 1;
$fieldname[$fc] = "wife_name"; $fc = $fc + 1;

}elseif ($now == "2"){
// PUT IN THE INFO FROM STEP 2
$fieldname[$fc] = "husband_age"; $fc = $fc + 1;
$fieldname[$fc] = "wife_age"; $fc = $fc + 1;
$fieldname[$fc] = "husband_race_caucasian"; $fc = $fc + 1;
$fieldname[$fc] = "husband_race_black"; $fc = $fc + 1;
$fieldname[$fc] = "husband_race_americanindian"; $fc = $fc + 1;
$fieldname[$fc] = "husband_race_asian"; $fc = $fc + 1;
$fieldname[$fc] = "husband_race_hispanic"; $fc = $fc + 1;

}elseif ($now == "3"){
// PUT IN THE INFO FROM STEP 3
$fieldname[$fc] = "husband_medhistory_acne"; $fc = $fc + 1;
$fieldname[$fc] = "husband_medhistory_bleeding"; $fc = $fc + 1;
$fieldname[$fc] = "husband_medhistory_thyroiddisease"; $fc = $fc + 1;
$fieldname[$fc] = "husband_medhistory_prostratedisease"; $fc = $fc + 1;
$fieldname[$fc] = "husband_medhistory_diabetes"; $fc = $fc + 1;

}

if (isset($fieldname[0])){
$fieldct = 0;
$fields = "";
foreach ($fieldname as $fn){

// SEPARATE FIELDS AND IND DATA ITEMS WITH COMMAS
if ($fieldct > 0) { $fields = $fields.","; }

// CREATE THE STRING FOR THE FIELD UPDATES
$fields = $fields.$fn." = '".addslashes($_POST[$fn])."'";

// INCREMENT THE FIELD COUNT
$fieldct = $fieldct + 1;
}

$updrec = "UPDATE donors SET ".$fields." WHERE donorid = ".$bdonorid;
mysql_db_query($dbname, $updrec) or die("Error: ".mysql_error());
}
}
?>

Weedpacket
12-04-2004, 01:47 AM
You can use [] to append items to an array.


$fieldname[] = "husband_name";
$fieldname[] = "wife_name";


Look, too, at array_merge.

planetsim
12-04-2004, 03:01 AM
weedpacket either has patience, had to work today or is just such a nice person id say 1st and last because i couldnt really understand much of it, please in future when posting PHP code or HTML to use either

for PHP and when using other code HTML etc.

Also if your going to do one liners


if (isset($_POST[now])) {$now == $_POST[now];}


Why not use


$now = (isset($_POST['now']))?$_POST['now']:false;

rehfeld
12-04-2004, 04:10 AM
you should also make sure you use use quotes for array indices like so



// do

$_POST['now']

// dont do

$_POST[now]

Weedpacket
12-04-2004, 06:27 AM
Originally posted by planetsim
weedpacket either has patience, had to work today or is just such a nice person id say 1st and last because i couldnt really understand much of it,...Nah, it was just that really distinctive texture. In fact, on second look I'd say that neither suggestion of mine is best. Those branches are exclusive, so

$fieldname = array(
'husband_name',
'wife_name'
);
etc. would be quite enough.