I have a form which is actually a table, the table (form) is dynamic, thus the table can consist of anything from 10 to 300 rows,
The form table:
Following is the code for the input form table:
include("x.xxx");
$cxn = mysqli_connect($host,$user,$password,$dbname);
$query = "SELECT * FROM `inspjc` ORDER BY `scaffreq_id`";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");
echo "<table align='center'><br>
<tr bgcolor='#8DB4E3'>
<th>X</th>
<th>Scaffold Req No</th>
<th>Elevation</th>
<th class='vertical'>Foundations</th>
<th class='vertical'>Ledgers</th>
<th class='vertical'>Face Brace</th>
<th class='vertical'>Plan Brace</th>
<th class='vertical'>Platforms</th>
<th class='vertical'>Mobiles</th>
<th class='vertical'>Uprights</th>
<th class='vertical'>Transoms</th>
<th class='vertical'>Transverse Braces</th>
<th class='vertical'>Ties</th>
<th class='vertical'>Safe Access</th>
<th class='vertical'>Signs</th>
<th>If Not Inspected<br>Supply a Reason</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr>\n
<td><input type='checkbox' checked='checked' name='scaffreq_id[]' value='$scaffreq_id' /></td>\n
<td><center>$scaffreq_id</center></td>\n
<td><center>$level m</center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp1[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp2[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp3[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp4[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp5[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp6[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp7[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp8[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp9[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp10[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp11[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp12[]' value='y' /></center></td>\n
<td><center><p><input type='text' name='insp_reason[]' maxlength='255' size='45' value='$insp_reason'></p></center></td>\n
</tr>\n";
}
echo "</table><br>";
?>
The desired output looks like this (simplified):
x ID a b c d e f g
---------------------------------------------------
1 365 1 1 0 0 0 1 North Bay
1 211 1 0 1 1 1 1 South Bay
0 237 0 1 1 1 1 0 Boiler
x represents an input type of type='checkbox' and ID represents its ID key. On submit, I would like to write the data to a database.
I have tried a few loop option, but I need the loop to run from php and not loop through my query (this takes too long), especially with 300 rows selected.
The basic outlay of the php code:
$ID = $_POST[ID];
$a = $_POST[a];
$b = $_POST[b];
$c = $_POST[c];
$d = $_POST[d];
$e = $_POST[e];
$f = $_POST[f];
$g = $_POST[g];
$userData = array();
foreach ($ID as $newid)
{
$userData[] = "('" . $newid['ID'] . "',
'" . $newid['a'] . "',
'" . $newid['b'] . "',
'" . $newid['c'] . "',
'" . $newid['d'] . "',
'" . $newid['e'] . "',
'" . $newid['f'] . "',
'" . $newid['g'] . "')";
}
$query = "INSERT INTO `inspect` (`ID`,`a`,`b`,`c`,`d`,`e`,`f`,`g`) VALUES ";
$query .= implode(',',$userData);
But, echo'ing the query only gives me "('0',0','0','0','0','0','0','0'),('0'..." yet there is exactly the quantity of data (within brackets) as the selected ID fields.