Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lilo's avatar
Level 2

How to create column in table using foreach

Hi,

I want to create a dynamic table according to the number of arrays that are returned. To be more specific, I get some data from database using a query and put the data in table. But if there is more than one array, I want a td tag created for each array and its data printed there.

Here is my code:

$analysisReports = $this->get_customer_data($id);
    //In $analysisReports, I get the data from database.
    $output='<html><body><div id="content">';

    foreach($analysisReports as $analysisReport) //When I put foreach like this, for each array a table is created. But what I want to do is create only one table.
    {
        $output .= '
            <html>
                <body>
                  <div id="content">
                      
                            <table width="100%">
                            
			//I want <td> to create as many columns as the returned array in analysisReports.
                            <tr><th>Row 1</th><td colspan="6">'.$analysisReport->row1.'</td><td colspan="1">Unit</td></tr>
                            <tr><th>Row2</th><td colspan="6">'.$analysisReport->row2.'</td><td colspan="1">Unit</td></tr>
                            </table>  
                             
                  </div>                              
                </body> 
            </html>

        ';
    }
    $output .= '</div></body></html>';
    return $output;

I hope I could explain it properly. Thanks in advance.

0 likes
7 replies
forrestedw's avatar

You can do this using the loop variable:

@foreach($analysisReports as $analysisReport)

    @if ($loop->first)
        $output .= // table headers, will only be added on first loop of foreach
    @endif

    $output .= // table row, will output for each loop

@endforeach
lilo's avatar
Level 2

Thanks for your help @forrestedw but I get this error: ErrorException Undefined variable: loop

And I wrote the foreach loop like this : foreach(){ }, because I get a syntax error when I write otherwise

Tray2's avatar

I take it you don't use blade.

Then you need to add a variable to see if it's the first itteration.

$first = true;


foreach($analysisReports as $analysisReport) {

    if ($first) {
        $output .= // table headers, will only be added on first loop of foreach
	$first = false;
    }

    $output .= // table row, will output for each loop

}
2 likes
bajjouayoub's avatar

@lilo I think your code in the controller, Right? if yes, as i know the the loop variable you can access it in the Blade files, so you will get an error !

bajjouayoub's avatar

So the Foreach do it inside the table tag, to create only one table and you can count the data you get and depend on that you can create the columns as per count.

bajjouayoub's avatar
Level 5

you can do like this as example:

$output';

$output.=' ';

			foreach($analysisReports as $inedx => $item) {

			$output.="
					<td>
						<th>$analysisReport->row . $index +1 .</th>
					</td>	
				"

			}

			foreach($analysisReports as $inedx => $item) {

			$output.="
					<td>
						<tr>unit. $index +1 .</tr>
					</td>	
				"

			}

$output ='

              </div>                              
            </body> 
        </html>

    ';
}
$output .= '</div></body></html>';
return $output;	

i hope this could help, modify this as your needs its just a general example

1 like

Please or to participate in this conversation.