Jump to content

[SOLVED] looping thru an array to populate a table


mahalleday

Recommended Posts

What i need to do is loop through an array that i take from a table in my database create/populate a table with its contents similar to this..

 

array[0]

array[1]

array[3]

array[4]

array[5]

array[6]

array[7]

array[8]

array[9]

 

here is the code i have so far but i can't get the result i need.

 

<?php
$j = 1;
while($cats = $cat_query->fetchRow()) {
    echo '<tr>';
    while($j <= $cols) {
       echo'<td><p><img src="" /><br/>'.$cats['cat_title'].'<br/>'.$cats['cat_desc'].'</p></td>';
       $j++;
    }
    $j = 1;
    echo '</tr>';
}
?>

 

I can make this work if it an array i created with php it self ie: $items = array("item1", "item2") and the referencing their positions in the array (0, 1, 2 etc...), but not when i create the array from a mysql table.

Try this:

 


<table>
<tr>
<?php
$column = 0;
$columns = 3;
while ($cats = $cat_query->fetchRow()) {
    if ($column and !($column % $columns)) {
        echo '</tr><tr>';
    }
    echo '<td><p><img src="" /><br/>'.$cats['cat_title'].'<br/>'.$cats['cat_desc'].'</p></td>';
    $column++;
}
for ($x = $column; $x < $columns; $x++) { echo '<td> </td>'; }
?>
</tr>
</table>


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.