Page 1 of 1
breaking the while loop each X entries
Posted: Tue Jul 06, 2010 9:06 am
by fael097
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
Re: breaking the while loop each X entries
Posted: Tue Jul 06, 2010 9:22 am
by Jade
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
Posted: Tue Jul 06, 2010 9:27 am
by fael097
thats it

thanky
Re: breaking the while loop each X entries
Posted: Tue Jul 06, 2010 2:45 pm
by Jonah Bron
Jade wrote:Code: Select all
if ($count % 4 == 0) //if you've gone through 4 records, start a new row
echo "</tr><tr>";
Just curious, why compare the remainder and not just $count >= 4? Please enlighten me.

Re: breaking the while loop each X entries
Posted: Tue Jul 06, 2010 2:54 pm
by Jade
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.
Re: breaking the while loop each X entries
Posted: Tue Jul 06, 2010 3:49 pm
by Jonah Bron
I see, so that just eliminates the need to reset the counter back to zero. Cool, I'll have to keep that in mind.
Re: breaking the while loop each X entries
Posted: Tue Jul 06, 2010 4:04 pm
by John Cartwright
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
Posted: Wed Jul 07, 2010 6:46 am
by fael097
John Cartwright wrote:Or, with the magic of CSS, you can eliminate the need for a counter at all (using floats and % width).
could you please give an example?

Re: breaking the while loop each X entries
Posted: Wed Jul 07, 2010 8:35 am
by Jade
John Cartwright wrote:Or, with the magic of CSS, you can eliminate the need for a counter at all (using floats and % width).
Haha true but CSS isn't my first choice for data that really should be in a table

Re: breaking the while loop each X entries
Posted: Wed Jul 07, 2010 12:25 pm
by Jonah Bron
fael097 wrote:could you please give an example?

For educational purposes only
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
Posted: Wed Jul 07, 2010 12:42 pm
by Jade
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>
Re: breaking the while loop each X entries
Posted: Wed Jul 07, 2010 12:52 pm
by Jonah Bron
Totally. That was just a demo. Even better, link to an external CSS file.