Sunday, 8 July 2012

Very simple but very powerful (Web Development)

Simple (but powerful) paging function

By Gewalop on Mar 4th, 2011 4:11 pm
Hi,
- Why use paging?
It's not really cool to print out all 2000 rows you have in a table to the page, so it'd be nice to page them.
Special cases?
Yeah, if you have 1000 rows.. and you've set the maximum row per page to 20, well.. you're gonna get 50 links for your pages.. so I took care of that. How? I only show the first costume number of page links, and the more you navigate, the more pages you'll see.
Parameters:
1- The maximum number of results you want in each page.
2- The maximum number of pages links you want in the page.
3- The query you've used to get your results.
4- The page that has your results.

Example (call the function in the place you want to put the page links)
  1. <?php
  2. pageThis(20, 10, "SELECT * FROM notes", notes.php);
  3. ?>
  1. //Preparing --Start--
  2. function pageThis($max, $max_page_links, $query, $page_link){
  3. //Uncomment the next 4 lines if don't wish to use this as a function
  4. //$page_link="paged.php";
  5. //$max=30;//Maximum items per page (rows)
  6. //$max_page_links=10;//Maximum links number in the page
  7. //$query="select * from some_table";//Your query
  8. $num_stuff=mysql_num_rows(mysql_query($query));//Getting the total possible rows
  9. if (isset($_GET['page'])&&$_GET['page']>0){
  10. $start=intval($_GET['page'])-1;//Now the page number make more sense (page=1 is actually the first page)
  11. $current_page=intval($_GET['page']);//Some cleaning (SQL Injection prevention, and get rid of negative numbers)
  12. }
  13. //If no parameters passed.. just give the first page
  14. else {
  15. $current_page=1;
  16. $start=0;
  17. }
  18. //If a large page numbre passed (more than there actually is) just give the last page
  19. if ($current_page>ceil($num_stuff/$max)){
  20. $current_page=ceil($num_stuff/$max);
  21. $start=ceil($num_stuff/$max)-1;
  22. }
  23. $start*=$max;//Which row to start with
  24. $get_stuff_query.=" limit $start, $max";//Adding the limit
  25. //Preparing --End--
  26. //Actual paging --Start--
  27. if ($num_stuff>$max){//Is there any need for pagin?
  28. if ($current_page>1){//Making previous page & first page links when needed
  29. $previous_page=$current_page-1;//previousious means -1
  30. echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=1\">|<</a>  ";//First page is the page number.. you guessed it right.. 1
  31. echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=$previous_page\"<<</a>  ";
  32. }
  33. if ($current_page>$max_page_links/2){//Are we going far away from the first viewed page link?
  34. if (ceil($num_stuff/$max)-$current_page<(($max_page_links/2)+1)){//Are we getting closer to last viewed page link?
  35. $start_counter=$current_page-($max_page_links-(ceil($num_stuff/$max)-$current_page));//Yes, Then we need to view more page links
  36. $end_counter=ceil($num_stuff/$max);//And no need to view page links more than the query can bring
  37. }else{
  38. $start_counter=$current_page-(($max_page_links/2)-1);//No, then just view some links before the currentrent page
  39. $end_counter=$current_page+($max_page_links/2);//And some links after
  40. }
  41. }
  42. else{//Still in the first pages?
  43. $start_counter=1;//Start with page one
  44. $end_counter=$max_page_links;//Show only enough links
  45. }
  46. for ($i=$start_counter;$i<=$end_counter;$i++){//A loop for viewing the links
  47. if ($i==$end_counter){//Is this the last link?
  48. if ($i==$current_page){//Are we actually on the last page? Because we don't need the | after the link
  49. echo "<a style=\"color:blue;\" class=\"link\">".$i."</a>";//Then make it look like we're on that page
  50. }else{
  51. echo "<a class=\"link\" href=\"".$page_link."page=".$i."\">".$i."</a>";//Well yeah, it's the last link.. but we're not there yet.
  52. }
  53. }else{//Not the last page you say.. mmm.. then print normally (with | after the link)
  54. if ($i==$current_page){//Are we vewing this page?
  55. echo "<a style=\"color:blue;\" class=\"link\">".$i."</a>  |  ";//Make us know it
  56. }else{//Not viewing.. just a normal link (the most common case here)
  57. echo "<a class=\"link\" href=\"".$page_link."page=".$i."\">".$i."</a>  |  ";//Nothing to say
  58. }
  59. }
  60. }
  61. if ($current_page<ceil($num_stuff/$max)){//Making the next and last page links
  62. $next_page=$current_page+1;//Next means +1
  63. $last_page=ceil($num_stuff/$max);//and the last page is the page.. whell.. it's the last one the query can bring
  64. echo "  <a style=\"text-decoration: none;\" href=\"".$page_link."page=$next_page\">>></a>";
  65. echo "  <a style=\"text-decoration: none;\" href=\"".$page_link."page=$last_page\">>|</a>";
  66. }
  67. }
  68. }

No comments:

Post a Comment