Jump to content

MySql Help


Go to solution Solved by Psycho,

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

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.