简体   繁体   中英

Use PHP while() only to return the first few values.

I am using PHP while() to fetch the results of a database query.

while($results = mysql_fetch_array( $galleryresult )) { // show the results };

However, I would like to paginate the results if they exceed a certain number of entries. So for example if there are more than 12 entries returned, only the first 12 will be shown.

1) How can I adapt while() so only the first 12 entries are returned on the first page.

2) Then how can I adapt while() so the 13th-24th result are returned on the second page and so on.

Modify the query such that only 12 entries are in the result set. That way you do not need to modify the while loop.

Add a parameter to the URL, that tells the script the offset that should be used in the SQL query.

Create a query that counts the total number of results. Use this to determine how many pages should be displayed in the pager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM