While Loop - how to break in groups of 10

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
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

While Loop - how to break in groups of 10

Post by michlcamp »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have table full of addresses and want to break them into groups of 10 addresses each...
don't know how to write the loop...

all I've got so far is:

Code: Select all

$query="SELECT 'address' FROM customers";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
echo "$address <br>";

$i++;
}
What I'd like to end up with is 10 addresses in a list then a space (line break), 10 more and a space...

All help appreciated...
thanks.mc


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$query="SELECT 'address' FROM customers";

$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;

while ($i < $num) 
{
    echo "$address <br>";
    if($i % 10 == 0)
        echo "<br><br>";
    $i++;
}
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

couldn't that to work as written..
thanks.mc
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

michlcamp wrote:couldn't that to work as written..
thanks.mc
Huh? :?
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

couldn't get that to work.
produced a blank page.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

No, it wouldn't - but your original code wouldn't do anything either.

Code: Select all

$query="SELECT 'address' FROM customers";

$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;

while ($a_row = mysql_fetch_object( $result ) )
{
    echo "$a_row->address<br>";
    if($i % 10 == 0)
        echo "<br><br>";
    $i++;
}
Assuming 'address' is the name of one of the fields in your 'customers' table.
Post Reply