0

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?

1 Answer 1

3
<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>
Sign up to request clarification or add additional context in comments.

1 Comment

Well, yes, I used just a placeholder. You're supposed to replace it with the actual loop you use to go through the query results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.