I have following PHP code contain 2 foreachs
echo "<table class='table table-bordered'>";
foreach($resultOld as $key=>$value)
{
foreach ($value as $key1 => $subjects) {
$checked = $subjects;
echo "<tr><tr class=\"".$subjects."\">$key1
<input type='checkbox' class=\"".$subjects."\" value='checked' name=\"".$key1."JAN\" $checked/> </tr>
</tr>" ;
}
}
echo "</table>";
$resultOld
is a fetchAll(PDO::FETCH_ASSOC)
output and it contains a two dimensional array. $subjects
will return 661 words from the database which means the $resultOld
array have 661 elements. And after every 12 cells I want to start a new line (tr
). That means I need 55 rows in the table. How to achieve this using PHP?
If you want a new row after every 12 records, you need to use a counter and check the count -
$counter = 1;
echo "<table class='table table-bordered'>";
echo "<tr>"; //start the first row
foreach($resultOld as $key=>$value)
{
foreach ($value as $key1 => $subjects) {
// if the 13th cell, end last row, and start new row
if ($counter%12==1){
echo "</tr><tr>";}
$checked = $subjects;
echo "<td class=\"".$subjects."\">$key1
<input type='checkbox' class=\"".$subjects."\" value='checked' name=\"".$key1."JAN\" $checked/> </td>" ;
// increase counter
$counter++;
}
}
echo "</tr>"; // end last row
echo "</table>";
First, note that your HTML structure is wrong. I recommend thinking about this is a little more steps. Perhaps layout a single row <table>
HTML structure for reference between slicing it up into PHP
Here's an example:
<table class="...">
<tbody> <!-- I recommend using the tbody tag -->
<tr>
<td>...</td>
</tr>
</tbody>
</table>
There's three basic things to think about:
Step 1 is to print the table body. You're doing fine here:
echo '<table class="table table-bordered">';
echo '<tbody>';
//...
<echo '</tbody>';
echo '</table>';
Step 2 is to loop through your rows
echo '<table class="table table-bordered">';
echo '<tbody>';
// Loop Through Rows
foreach($resultOld as $key=>$value)
{
echo '<tr>'; // start a new row
// ...
echo '</tr>'; // end a row
}
<echo '</tbody>';
echo '</table>';
Step 3 is to loop through each column or cell of the table:
// STEP 1
echo '<table class="table table-bordered">';
echo '<tbody>';
// STEP 2
// Loop Through Rows
foreach($resultOld as $key=>$value)
{
echo '<tr>'; // start a new row
// STEP 3
foreach ($value as $key1 => $subjects) {
$checked = $subjects;
// Start a new column/cell
echo '<td class="' . $subjects . '">';
// Print cell contents
echo $key1;
echo '<input type="checkbox" class="' . $subjects . '" value="checked" name="' . $key1 . 'JAN" '. $checked . '/>';
// End column/cell
echo '</td>';
} // END STEP 3
echo '</tr>'; // end a row
} // END STEP 2
echo '</tbody>';
echo '</table>';
// END STEP 1
Some notes on your code:
<tr><tr>
instead of <tr><td>
;For me, I like to use single quotes (') with printing HTML. I do this because I use double quotes (") for HTML attributes. This allows me to avoid having to escape the double quote character and getting a hard to read '\""' situation, which can cause simple syntax bugs.
Also when running into problems printing HTML, braking the HTML into multiple echo/print statements can help you structure your code and troubleshoot the problems. Once it's working you can go back and refactor them into a single echo statement, however the performance difference would probably so minor that it's not worth the time.
I hope that helps!