Page 1 of 1

While Loops

Posted: Thu Aug 07, 2003 2:02 pm
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

Posted: Thu Aug 07, 2003 3:01 pm
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.

Posted: Thu Aug 07, 2003 3:06 pm
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

Posted: Thu Aug 07, 2003 3:14 pm
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.