breaking the while loop each X entries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

breaking the while loop each X entries

Post 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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: breaking the while loop each X entries

Post 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
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

Re: breaking the while loop each X entries

Post by fael097 »

thats it :)
thanky
User avatar
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

Post 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. :)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: breaking the while loop each X entries

Post 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.
User avatar
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

Post 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.
User avatar
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

Post by John Cartwright »

Or, with the magic of CSS, you can eliminate the need for a counter at all (using floats and % width).
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

Re: breaking the while loop each X entries

Post 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? :)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: breaking the while loop each X entries

Post 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 :)
User avatar
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

Post 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>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: breaking the while loop each X entries

Post 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>
User avatar
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

Post by Jonah Bron »

Totally. That was just a demo. Even better, link to an external CSS file.
Post Reply