While Loops

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

While Loops

Post by Jade »

Hello,

Does anyone know the code to make a while loop not start at the very first record, but to start at a record you specify?

Thanks,
Jade
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You mentioned records so I assume you are asking about while loops applied to a database result resource.

The mysql_data_seek() fn can move the internal result resource pointer on to the nth row - assuming that you do have a value for $n of course.

If not, post again since I'd need to know more about the specific script.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Yep thats what i mean, but can you put it in an example loop so i can see exactly how its used? Thanks for your time,

Jade
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Well.. usually the idea is to help people help themselves. Have you tried it yourself yet?

Anyway, this is how it goes:

Code: Select all

<?php

// somewhere, somehow, you get/create an integer var to pass to mysql_data_seek()
$pos = 3;
// .. and your result resource ($query) has also been declared..

// next:
mysql_data_seek($query, $pos);


// the while loop now begins from $pos: note that the first row is $pos = 0 and NOT 1
while($result = mysql_fetch_assoc/row/array($query))
{
    // ... etc
}
?>
Do you have a copy of the php manual? If not, I'd recommend downloading the version with user comments from php.net. Hope this helps.
Post Reply