I've built a page for my company's managers that uses checkboxes to authorize commission requests submitted by employees. It does a MySQL query and formats a list showing a description of the commission request, and then provides a checkbox to either accept or deny their request.
Because there's 2 checkboxes bound to each row, I needed someway to differentiate each returned row's checkboxes from the previous and following rows. So I set an auto-incrimenting variable (which was being used anyway) to slap a number on the checkboxes. So on a two row return, I would have
$id1 for an accepted check, row 1.
$id2 for an accepted check, row 2.
$deny1 for a denied check, row 1.
$deny2 for a denied check, row 2.
Then you select whichever options you want, and it posts the form. The problem arises in the action part of the script. I had to figure out a way to perform a while function for each of the incrimenting variables. And I still have no real way to make it automatically incriment. What I ended up doing was sticking a static amount of variables into an array and then using a foreach command.
My question is if there's a cleaner way to make it automatically incriment a variable number (go from $id1 to $id2 if ($id2 != "") ) or if I have to stick to a static assignment. I'll past my form script, and my action script so you can see what I did. I hope the notation doesn't get too munged when I post. 🙂
Thanks for any thought put to the question.
while ($data = mysql_fetch_array($results)) {
$i++; $currentcolor = $i % 2;
if ($currentcolor==0) { $bgcolor="lightblue"; } else { $bgcolor="#98ACC8"; }
print "<TR bgcolor=".$bgcolor.">\n";
print "<TD align=\"center\">".$data[employee]."</TD>\n";
print "<TD align=\"center\">".$data[timestamp]."</TD>\n";
print "<TD align=\"center\">".$data[description]."</TD>\n";
print "<TD align=\"center\">$".$data[value]."</TD>\n";
print "<TD align=\"center\"><INPUT type=\"checkbox\" name=\"id".$i."\" value=\"".$data[id]."\">\n";
print " / <INPUT type=\"checkbox\" name=\"deny".$i."\" value=\"".$data[id]."\">\n";
print "</TD>";
print "</TR>";
}
Action:
$id = array ($id1, $id2, $id3, $id4, $id5, $id6, $id7, $id8, $id9, $id10);
foreach ($id as $currentid) {
if ($currentid == "") { } else { //perform function here }