Posted By

drifterz28 on 06/21/11


Tagged

pagination


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

dmanjdb
tux-world
pruntrut


Simple and easy PHP pagination


 / Published in: PHP
 

URL: http://e-commsolution.com

  1. // paging code
  2. // Query to count rows.
  3. $result = mysql_query("SELECT * FROM Table_Name WHERE Column_Name = '$section'");
  4. $items = 32; // number of items per page.
  5. $all = $_GET['a'];
  6.  
  7. $num_rows = mysql_num_rows($result);
  8. if($all == "all"){
  9. $items = $num_rows;
  10. }
  11. $nrpage_amount = $num_rows/$items;
  12. $page_amount = ceil($num_rows/$items);
  13. $page_amount = $page_amount-1;
  14. $page = mysql_real_escape_string($_GET['p']);
  15. if($page < "1"){
  16. $page = "0";
  17. }
  18. $p_num = $items*$page;
  19. //end paging code
  20. // Query that you would like to SHOW
  21. $result = mysql_query("SELECT * FROM Table_Name WHERE Column_Name = '$section' ORDER BY 'name' ASC LIMIT $p_num , $items");
  22.  
  23. function paging(){
  24. global $num_rows;
  25. global $page;
  26. global $page_amount;
  27. global $section;
  28. if($page_amount != "0"){
  29. echo "<div class=paging>";
  30. if($page != "0"){
  31. $prev = $page-1;
  32. echo "<a href=\"section.php?q=$section&p=$prev\">Prev</a>";
  33. }
  34. for ( $counter = 0; $counter <= $page_amount; $counter += 1) {
  35. echo "<a href=\"section.php?q=$section&p=$counter\">";
  36. echo $counter+1;
  37. echo "</a>";
  38. }
  39. if($page < $page_amount){
  40. $next = $page+1;
  41. echo "<a href=\"section.php?q=$section&p=$next\">Next</a>";
  42. }
  43. echo "<a href=\"section.php?q=$section&a=all\">View All</a>";
  44. echo "</div>";
  45. }
  46. }
  47. // call on Pagination with function
  48. paging();

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Seth_Angel on October 11, 2012

What's the a in $all = $GET['a']; and the p in $page = mysqlrealescapestring($_GET['p']); supposed to be?

Posted By: drifterz28 on December 18, 2012

That is the "View All" pram and it is escaped to prevent MySQL injection.

You need to login to post a comment.