Help with simple loop


#1

Hi everyone:

I have a beginers question and any help is greatly appreciated. I just started writing small programs and I in the middle of one.

I am using the following loop to extract a list of filenames from an MySQL database:
while ($row = mysql_fetch_array($result))
{…
it results in a list of more than 20 files. I would like to display those images four at a time. That is create a table of images and only display four at a time with a next button at the bottom that when pressed displayes the next four and so on and so on until all the images have been displayed. I know that there is probably a simple loop that woudl take care of this but I cant for the life of me figure out.

Any help would be greatly apprecited.

Celia


#2

I don’t think it’s the loop as much as it is the query.

if you use the LIMIT statement you can limit the query to those records you want. The limit keyword has 2 parts. The start record and the number of records

For example the following will return all records based on a condition.
SELECT * FROM some_table WHERE some_condition=some_value ORDER BY some_key;

If the above gave too many you can LIMIT it:
SELECT * FROM some_table WHERE some_condition=some_value ORDER BY some_key LIMIT 0,4 ;
The above will only give you the first 4 records starting at the beginning of the record set. You would probably need to set up a GET variable to tell it to get the NEXT record set. Then you could use the NEXT query:

SELECT * FROM some_table WHERE some_condition=some_value ORDER BY some_key LIMIT 4,4 ;

Now you will get 4 more records starting at record number 4 (or the 5th record counting 0).

Make sense?


#3

Thanks! I was able to finish up my dynamic page with your help. I really appreciate it!

Celia