kevinbo kevinbo - 7 months ago 14
HTML Question

Displaying variables in HTML table?

I currently have a simple table inside my HTML as followed:

<table style="width:100%">
<tr>
<td>test</td>
<td>test2</td>
<td>test3</td>
</tr>
</table>


I also have below there, this part of php code:

echo '<pre>';
echo '==================================';
var_dump($output_filtered);
echo '==================================';


This isn't actually important, but i thought I'd still provide it.

Here is the result as it may be necessary to see:

array(10) {
[0]=>
object(stdClass)#1 (6) {
["id"]=>
int(105740)
["title"]=>
string(30) "Aanpassingen website Veerhaven"
["phase"]=>
string(6) "active"
["start_date"]=>
int(1456787280)
["start_date_formatted"]=>
string(10) "01/03/2016"
["cf_value_26656"]=>
string(37) "Opzet & webdevelopment (Eenmalig)"
}
[1]=>
object(stdClass)#2 (6) {
["id"]=>
int(99141)
["title"]=>
string(14) "Alindo website"
["phase"]=>
string(6) "active"
["start_date"]=>
int(1454368080)
["start_date_formatted"]=>
string(10) "02/02/2016"
["cf_value_26656"]=>
string(37) "Opzet & webdevelopment (Eenmalig)"
}


Now is my question, how can i get $output_filtered inside my table?

Thanks in advance,

K.

Answer

First iterate through the array, and create a table row for each object then use the objects' properties where they are needed.

<?php if(isset($output_filtered)) { ?>
<table style="width:100%">
<?php
  foreach($output_filtered as $output_obj){
    echo "<tr>";
    echo "<td>" . $output_obj->title . "</td>";
    echo "<td>" . $output_obj->phase . "</td>";
    echo "<td>" . $output_obj->start_date_formatted . "</td>";
    echo "<td>" . $output_obj->cf_value_26656. "</td>";
    echo "</tr>";
  }
?>
</table>
<?php 
 }else{
  echo 'ERROR - $output_filtered was not set';
 }
?>