Vince Agno
Vince Agno

Reputation: 81

Looping a table to display results in a row

I would like to display contents of a table that would be in a row. as of now my codes generate it in a column, vertically. How can I fix it so that it would be side-by-side when generated.

or at least if it's more than 2

2 columns maximum, and then another row for the other values then after 2 columns, another row.

Please help me display both. So I can see which is better.

Here is my code:

<table width="" cellpadding="3" cellspacing="0" border="0">
<?php
    $qry="SELECT * FROM shipping";
    $result= @mysql_query($qry);
    while ($row=mysql_fetch_assoc($result)){
    ?>
        <tr class="row_submit">
            <td height="250px" width="300px"><center><label>
                <input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="row[ship_name]") echo "checked";?>  value="<?php echo $row['ship_name']; ?>">
                <!--<img src="../paymentoptions/lbc.png" alt="LBC" class="picture" width="245px" style="margin:10px"/></label></td> -->
                <?php echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['ship_pic'] ).'"  height="210px"  width="245px" style="margin:10px"/>'; ?>

            <td></td>

        </tr>
        <tr class="row_submit">
            <td height="180px" width="300px"><p><?php echo $row['ship_desc']; ?><p>
            <div id='price'> Additional ₱ <?php echo $row['ship_price']; ?></div></td>
            <td></td>

    <?php } ?>

    </table>

Upvotes: 0

Views: 77

Answers (1)

Mark Miller
Mark Miller

Reputation: 7447

This will display your data in rows of two columns each:

<?php
echo "<table><tr>";
$i = 0;
while (...) { 
    if ($i > 0 && $i % 2 == 0) { 
        echo "</tr><tr>";
    }
    echo "<td>...</td>";
    $i++;
}
echo "</tr></table>";
?>

Result:

***************
Item1  |  Item2
Item3  |  Item4
Item5  |  Item6
....   |  ....
***************

To display your data side-by-side, in one single row, it is much simpler:

<?php
echo "<table><tr>";
while (....) { 
    echo "<td>...</td>";
}
echo "</tr></table>";
?>

Result:

*****************************************************************************
Item1  |  Item2  |   Item3  |  Item4  | Item5  |  Item6  |  ....  |  ItemN  |  
*****************************************************************************

Upvotes: 1

Related Questions