Jump to content

MySql Help


MstrGmrDLP

Recommended Posts

Okay, I am having a little trouble with a mysql table. I am trying to make a table. I have a <tr> in a while statement. I am trying to make it so that it will go and display 5 users across the <tr> and then it will close the tr and start a new one and repeat showing the next 5 users. What would the code for this be? Any help would be appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/287687-mysql-help/
Share on other sites

Keep a counter that starts at 1 and increments for every table cell you output. At the beginning of the loop, if the value is 1 then output a

. At the end of the loop, when the (new) value is 5 then output a and reset to 1, otherwise just increment.

 

After the loop be sure to clean up any incomplete row you may be in the middle of outputting: if the counter is >1 then output however many cells it takes to get the counter back up to 5 (keeping in mind that counter=2 means you need four cells, not three).

Link to comment
https://forums.phpfreaks.com/topic/287687-mysql-help/#findComment-1475736
Share on other sites

I prefer to let the counter just increment by one on each iteration and not reset it. Instead, you can just use the modulus operator. Example:

 

$columns = 5;
$count = 0;
 
while($row = mysql_fetch_assoc($result))
{
    $count++;
 
    //Open new row if needed
    if($count%$columns == 1)
    {
        echo "<tr>\n";
    }
 
    //Display the record
    echo "<td>";
    //Record output goes here
    echo "</td>";
 
    //Close the row if needed
    if($count%$columns == 0)
    {
        echo "</tr>\n";
    }
}
 
//After loop, close row if it didn't end with full last row
if($count%$columns != 0)
{
    echo "</tr>\n";
}
Link to comment
https://forums.phpfreaks.com/topic/287687-mysql-help/#findComment-1475744
Share on other sites

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.