By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

limit search results so that there is a "Next 10" and "Previous 10"?

P: 1
Hi there...

How can I limit search results so that there is a "Next 10" and "Previous 10"?

thanks for any reply....
Apr 18 '08 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
Hi. Welcome to Bytes!

After trying, and failing, to explain this a few times, I feel a simple code example will be more helpful :)

Consider this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Get the current page index, sent thought the URL
  3. $pageIndex = @$_GET['pindex'];
  4.  
  5. // If there was no index in the URL, set it to 0
  6. if(empty($pageIndex))
  7.   $pageIndex = 0;
  8.  
  9. // Set some basic variables needed throughout the script
  10. $amountPerPage = 10;
  11. $nextPage = $pageIndex + 1; // The number for the Next link
  12. $prevPage = $pageIndex - 1; // The number for the Prev link
  13. $firstItem = $pageIndex * $ammountPerPage;
  14.  
  15. // Get the data from where ever you get you data from
  16. // ... compact MySQL example 
  17. $result = mysql_query("SELECT field FROM table LIMIT {$firstItem}, {$ammountPerPage}");
  18.  
  19. // Show the data
  20. // ... for example
  21. forech ($row = mysql_fetch_assoc($result)) {
  22.   echo "Field = ". $row['field'] ." <br />";
  23. }
  24.  
  25. // Show the Next and Prev links
  26. echo "<a href='?pindex={$prevPage}'>Previous {$ammountPerPage}</a> | 
  27.       <a href='?pindex={$nextPage}'>Next {$ammountPerPage}</a>";
  28. ?>
  29.  
The index of the page that is to be displayed is simply added to the URL as a GET variable, and then retrieved through the $_GET array. If there is no variable passed the page index will be set to 0 (the first page).

This piece of code is obviously pretty basic and will have to be improved and adjusted to your needs before it is put to use, but you get the idea.
Apr 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.