Alright.. lets get down to business!
I working on a report log for my fire company, it keeps track of all the calls we get, as well as keeps track of each member at our company and how many calls he makes. Makes it nice at the end of the year for totaling all this data.. Plus it's a great asset when applying for grants! 🙂
You may want to check out my previous post
I'm still trying to figure out the most efficient way to collect the data from these multipul select boxes. I use the following code to create <select> boxes for each one of the members in our Fire Company.
// Get Member Names & info From Database so it can be updated
$query = "SELECT * FROM members ORDER BY lastname";
// Place into niice tables!
echo "<div align=\"center\"><table width=\"75%\" border=\"1\" bordercolor=\"#FFFFFF\">";
$j = 0;
$rownum = 0;
$result = mysql_query($query);
if (($result = @ mysql_query ($query, $connection))){
while ($row = mysql_fetch_array($result)){
if($j == $rownum) {
echo "<tr>";
$rownum += 3; //<------- NUMBER OF RECORDS PER ROW
}
echo "<td width=\"30%\"><font size=\"2\" face=\"Tahoma\">
<select name=\"".$row['id']."\">
<option>-----------</option>
<option name=\"$row['fire']\" >Fire Scene</option>
<option name=\"$row['drill']\" >Drill Scene</option>
<option name=\"$row['station']\" >Station</option>
</select>
" .$row['lastname'] . ", " . $row['firstname'] . "</td>";
++$j;
}
if($j == $rownum) {
echo "</tr>";
}
}
echo "</table></div>";
?>
You Can See this in action here CVFC Report Log it's located at the bottom of the page.
Now the Problem lies with the collecting of this data.
There are a few obsticales to surpass. there are three conditions for each member if there is data sent(if a selection is made). Here is the BASE code for that.
// Each Varible into the $fireVars['var'] Format
foreach($_POST as $varname => $value)
$fireVars[$varname] = $value;
// For each member check if he made the call
// and if he did, update the database accordingly
if ($fireVars[''] == 'fire'){
// update members database for the scene
$sql = mysql_query("UPDATE members SET scene = scene + 1") or die(mysql_error());
}elseif ($fireVars[''] == 'station'){
// update members database for the station
$sql = mysql_query("UPDATE members SET station = station + 1") or die(mysql_error());
}elseif ($fireVars[''] == 'drill'){
// update members database for the drill
$sql = mysql_query("UPDATE members SET drills = drills + 1") or die(mysql_error());
}
I need some type of loop to do this for each member in the database, i can't do this seperatly for each member because new members may join and other members may leave the company. Not to mention how lengthy the code would get.
The user's id from the database is what's being sent to the 'collection' script (script collecting the data), which is nothing more than a 'auto_increment' integer.
So with all that said. What would be an efficient way to make updates to the members database?
How should i construct a loop and send each variable of each member through the loop? i was thinking along the lines of foreach? but i don't know how to construct it properly for what i need it to do
I really do appreciate your help! and Thank-you!