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.
@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
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
}
@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 !
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.