Next and Previous links/limiting results


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $sort_by = ($_GET["sort_by"]) ? $_GET["sort_by"] : "created";
  2. $sort_order = ($_GET["sort_order"]) ? $_GET["sort_order"] : "DESC";
  3. $opposite_sort_order = ($sort_order == "ASC") ? "DESC" : "ASC";
  4. $start = ($_GET["start"]) ? $_GET["start"] : 0;
  5. $results_limit = 10;
  6.  
  7. ...
  8.  
  9. $total_results = mysql_num_rows($result);
  10. $query .= "ORDER BY " . $sort_by . " " . $sort_order . " LIMIT " . $start . ", " . $results_limit . ";";
  11.  
  12. ...
  13.  
  14. if ($start > 0) {
  15. ?>
  16. <p style="float: left;"><a href="?sort_by=<?=$sort_by?>&sort_order=<?=$sort_order?>&start=<?=$start - $results_limit?>">&lt; Previous</a></p>
  17. <?php
  18. }
  19. if (($start + $results_limit) < $total_results) {
  20. ?>
  21. <p style="float: right;"><a href="?sort_by=<?=$sort_by?>&sort_order=<?=$sort_order?>&start=<?=$start + $results_limit?>">Next &gt;</a></p>
  22. <?php
  23. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.