php: output Array in html table -
how can write values into html table?
this array:
array ( [data] => array ( [0] => array ( [0] => [1] => sortprice [2] => sortdate ) [1] => array ( [0] => lorem ipsum [1] => 1. preis [2] => 1998 ) [2] => array ( [0] => b lorem ipsum [1] => 2. preis [2] => 1997 ) [3] => array ( [0] => c lorem ipsum [1] => 3. preis [2] => 1996 ) ) )
i can output values. this:
foreach ($table['data'] $v1) { foreach ($v1 $v2) { echo "$v2\n"; } }
how can access values $ table ['data'] [1] .. foreach? write them table cell this:
<table> <tr> <td>a lorem ipsum</td> <td>1. preis</td> <td>1998</td> </tr> ..... </table>
as have column headers embedded within data, i'd print table header first , loop through rest of data.
<?php $table = [ 'data' => [ [ 'column 1', 'column 2', 'column 3' ], [ 'foo', 'bar', 'baz' ], [ 'big', 'fat', 'mamma' ] ] ]; $data = $table['data']; ?> <table> <thead> <tr> <th><?= $data[0][0] ?></th> <th><?= $data[0][1] ?></th> <th><?= $data[0][2] ?></th> </tr> </thead> <tbody> <?php array_shift($data); foreach($data $value) { ?> <tr> <td><?= $value[0] ?></td> <td><?= $value[1] ?></td> <td><?= $value[2] ?></td> </tr> <?php } ?> </tbody> </table>
output:
<table> <thead> <tr> <th>column 1</th> <th>column 2</th> <th>column 3</th> </tr> </thead> <tbody> <tr> <td>foo</td> <td>bar</td> <td>baz</td> </tr> <tr> <td>big</td> <td>fat</td> <td>mamma</td> </tr> </tbody> </table>
Comments
Post a Comment