Reputation: 824
I have a small shopping cart I am writing, and currently am facing problems on how to display MySQL table results into a tabulated display. I have a code that does the whole database search and returns results, my problem is displaying the results like most shopping carts do.
Here is an example of what I would like to achieve.
<table>
<tr>
<td>First mysql result row comes here</td>
<td>Second Mysql result row comes here</td>
</tr>
<tr>
<td>third mysql result row comes here</td>
<td>fourth Mysql result row comes here</td>
</tr>
</table>
Note that it displays two <td>
per line.
I am having a tough time trying to find this out. Could it be done using css?
Upvotes: 0
Views: 408
Reputation: 33163
<table>
<?php
$tr = false;
foreach( $results_from_database as $result ) {
$tr = !$tr;
if( $tr ) {
echo '<tr>';
}
echo "<td>$result</td>";
if( !$tr ) {
echo '</tr>';
}
}
// if there's odd amount of results, add an empty cell and close the tr tag
if( $tr ) {
echo '<td></td></tr>';
}
?>
</table>
Upvotes: 3