breaking the while loop each X entries
Moderator: General Moderators
breaking the while loop each X entries
hi, my question is simple, and i have no clue how to achieve that.
i made a while(mysql fetch array) loop to put each database result on a <td>, but i want it to make a new <tr> each 4 tds, how to achieve that?
thanks in advance
i made a while(mysql fetch array) loop to put each database result on a <td>, but i want it to make a new <tr> each 4 tds, how to achieve that?
thanks in advance
Re: breaking the while loop each X entries
Code: Select all
<?php
$count = 0;
$loop = mysql_query("your query here")
or die (mysql_error());
echo "<table><tr>"; //start the table and the first row
while ($row = mysql_fetch_array($loop))
{
if ($count % 4 == 0) //if you've gone through 4 records, start a new row
echo "</tr><tr>";
echo "<td>" . $row['data_field_here'] . "</td>";
$count++;
}
echo "</tr></table>"; //close the last table row
Re: breaking the while loop each X entries
thats it 
thanky
thanky
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: breaking the while loop each X entries
Just curious, why compare the remainder and not just $count >= 4? Please enlighten me.Jade wrote:Code: Select all
if ($count % 4 == 0) //if you've gone through 4 records, start a new row echo "</tr><tr>";
Re: breaking the while loop each X entries
If you do $count > 4 then you'll get four cells on the first row (which is what he wants) and then one cell per row when count > 4 instead of 4 cells per row. You only want a new row every 4 records so the $count should be divisible by the number of cells per row.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: breaking the while loop each X entries
I see, so that just eliminates the need to reset the counter back to zero. Cool, I'll have to keep that in mind.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: breaking the while loop each X entries
Or, with the magic of CSS, you can eliminate the need for a counter at all (using floats and % width).
Re: breaking the while loop each X entries
could you please give an example?John Cartwright wrote:Or, with the magic of CSS, you can eliminate the need for a counter at all (using floats and % width).
Re: breaking the while loop each X entries
Haha true but CSS isn't my first choice for data that really should be in a tableJohn Cartwright wrote:Or, with the magic of CSS, you can eliminate the need for a counter at all (using floats and % width).
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: breaking the while loop each X entries
For educational purposes onlyfael097 wrote:could you please give an example?
Code: Select all
<div id="table">
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
<div style="float: left; width: 25%; border: solid 1px #000000;">Some stuff</div>
</div>Re: breaking the while loop each X entries
A better way to do it is to make a class, that way you cut down on memory and the page will load faster.
<style type="text/css">
#table.cell {
float: left;
width: 25%;
border: solid 1px #000000;
}
</style>
<div id="table">
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
</div>
<style type="text/css">
#table.cell {
float: left;
width: 25%;
border: solid 1px #000000;
}
</style>
<div id="table">
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
<div class="cell">Some stuff</div>
</div>
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: breaking the while loop each X entries
Totally. That was just a demo. Even better, link to an external CSS file.