Loop PHP Array by Columns array and values in one table row?

Hello,

I am trying to loop through an array and populate a html table. But the values are each in a seperate row and not in one row.
Logic:
Loop through a feedback array and display if period_id == the column head period_id

This the code I have so far:

/***
 * Populate Table
 */

$colHeads = [
   [
            'period_id'=> 14,
            'period' => 'p1',
       ],
   [
            'period_id'=> 1,
            'period' => 'p2',

       ],
   [
            'period_id'=> 2,
            'period' => 'p2',

       ],
   [
            'period_id'=> 4,
            'period' => 'p3',

       ],
   [
            'period_id'=> 5,
            'period' => 'p5',

       ],
   [
            'period_id'=> 6,
            'period' => 'p6',
       ],
   [
            'period_id'=> 7,
            'period' => 'p7',
       ],
   [
            'period_id'=> 8,
            'period' => 'p8',

       ],
   [
            'period_id'=> 9,
            'period' => 'p9',

       ],
   [
            'period_id'=> 10,
            'period' => 'p10',

       ],
   [
            'period_id'=> 11,
            'period' => 'p11',
       ],

];

//echo '<pre>';
//print_r($colHeads);
//echo '</pre>';

$dailyFeedbacks = [
    [
        'date' => '2017-01-18',
        'period_id' => 4,
        'feedback_score' => 4,
        'feeback_notes' => 'test is pos feedack',
    ],
    [
        'date' => '2017-01-18',
        'period_id' => 6,
        'feedback_score' => 2,
        'feeback_notes' => 'test is asdf feedack',
    ],
    [
        'date' => '2017-01-18',
        'period_id' => 10,
        'feedback_score' => 2,
        'feeback_notes' => 'test is asdf feedack',
    ]
];

    $tableRow ='<table>';
    $tableRow .= '<tr><td>Feedback</td>';
    
    $i = 0;
    foreach ($colHeads as $headCol) {
     foreach($dailyFeedbacks as $dailyFeedback) {
        if ($i == 11) {
                    $i = 0;
                }
             $periodHead = $headCol['period_id'];
            if ($dailyFeedback['period_id']==$periodHead ) {
                $tableRow .= '<td>'.$dailyFeedback['feeback_notes'].'</td>';
            }  else {
                $tableRow .= '<td>&nbsp;</td>';
            }
            $i++;
                if($i == 11){
                    $tableRow .= '</tr>';
                }
        }   
    }
    $tableRow .= '</table

Hi @ArrayNoob and welcome to the forums.

Try this on the last line:

  echo $tableRow .= '</table>';

Edit:
Try adding “\n\t” because it will add line-feeds and tabs to the browser viewed output and maybe make debugging easier:

 $tableRow .= "\n" .'<tr><td>Feedback</td>';
...
  $tableRow .= "\n\t" .'<td>'.$dailyFeedback['feeback_notes'].'</td>';
...
 $tableRow .= "\n\t" .'<td>&nbsp;</td>';
...
$tableRow .= "\n\t" .'</tr>';
...

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.