Jump to content

Creating a two column table through looping


scrubbicus

Recommended Posts

Hey, so I have some categories that I have stored in a MySQL table. I want to loop through that table but create a two column menu. I tried this.

 

while($row = mysql_fetch_array($results)) {

<tr>

<td>

$row['name'];

</td>

<td>

$row['name'];

</td>

</tr>

}

 

but as I found out, and should have been obvious to me, it just gives that name twice so I have two of the same category on each row.

Hi,

Try this:

 

<?php
$count=1;
$number_of_columns=2; //The required number of columns
while ($rowWork = mysql_fetch_array($resultWork)){   
    if ($count==1){
        echo "<tr>"; //Open the row
    }
    echo "<td valign=\"top\">".$row['column']."</td>";
    if ($count==$number_of_columns){
        echo "</tr>"; //Close the row if we have reached the required number of columns
        $count=0; //Set the counter back
    }   
    $count++;
} 
?>

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.