0

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?

8
  • Why do you have 2 foreach blocks? use one foreach($resultOld['subjects'] as $subject){ ...... ...... } Commented May 22, 2013 at 19:10
  • use once td and once tr... Commented May 22, 2013 at 19:11
  • @SamBattat fetchAll(PDO::FETCH_ASSOC) is return TWO-DIMENSIONAL ARRAY Commented May 22, 2013 at 19:11
  • Use foreach($resultOld['subjects'] as $subject){....} Commented May 22, 2013 at 19:13
  • also this is wrong html <tr><tr class=\"".$subjects."\">$key1 should be only one <tr><td>content</td></tr> Commented May 22, 2013 at 19:14

2 Answers 2

2

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>";
Sign up to request clarification or add additional context in comments.

Comments

1

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:

  1. The Table Body
  2. The Row
  3. The Columns (ie. Cells)

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:

  • You're printing 2 Table rows instead of printing a table row and a table column (ie: <tr><tr> instead of <tr><td>;
  • You need to print the row inside the first loop, not the second loop.
  • The second, nested, loop prints out your 2nd dimension or columns.

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!

3 Comments

it is containing 661 cell in a signal raw but i need 12 columns and 55 raws
<echo '</tbody>'; remove < in the echo
Thanks. I missed that part originally and I'm re-reading your question. I'm a bit confused as you state $resultOld has 661 entries but your code shows you really looping over the value of $resultOld. Could you update your question with a single row of $resultOld? Using something like var_dump() or print_r()? I think I need to see your data structure. There might be a diff. loop structure with only 1 loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.