Advertisement
cdsatrian

simple sample paging

Oct 18th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  1. <?php
  2. session_start();
  3. /************************
  4. - SIMPLE SAMPLE PAGING -
  5. note        : not yet tested,
  6.               not yet finished
  7.               oh no! ^_^V
  8. created by  : CAHYA DSN
  9. created date: 2012-10-19
  10. *************************/
  11. //Database Configuration
  12. //-------------------
  13. $dbhost='localhost';
  14. $dbuser='test';
  15. $dbpass='test';
  16. $dbname='test';
  17. //Database connection
  18. //-------------------
  19. $db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  20. //-- get 1st content id for current page
  21. //--------------------------------------
  22. $page=isset($_GET['page'])?$_GET['page']:0;
  23. $id=0;
  24. $next_id='';
  25. if(isset($_SESSION['pages']) && !empty($_SESSION['pages']))
  26. {
  27.   $id=$_SESSION['pages'][$page];
  28.   if($_SESSION['pages'][$page]<$_SESSION['pagenum'])
  29.   {
  30.     $next_id=' AND id<'.$_SESSION['pages'][$page+1];
  31.   }
  32. }
  33. //show content per page
  34. //-------------------
  35. $sqldetail = 'SELECT id,title,content FROM table WHERE id>='.$id.$next_id.' ORDER BY id DESC';
  36. if ($details = $db->query($sqldetail))
  37. {
  38.    while($row=$details->fetch_object())
  39.    {
  40.      echo "<div class=\"content_box\">\n"
  41.          ."<div class=\"content_title\"><a href=\"?detail=".$row->id."\">".$row->title."</a></div>\n";
  42.          ."<div class=\"content\">".$row->content."</div>\n"
  43.          ."</div>";
  44.    }
  45.    $details->close();
  46. }
  47. //build paging navigation
  48. // paging model : 1|2|3|4|5
  49. //-------------------
  50. $perpage=5; //number of contents to be displayed per page
  51. if(!isset($_SESSION['pages']) || empty($_SESSION['pages']))
  52. {
  53.   $sqlpaging = 'SELECT id FROM table ORDER BY id DESC';
  54.   if ($result = $db->query($sqlpaging))
  55.   {
  56.     $num=$result->num_rows;
  57.     if(num>0)
  58.     {
  59.       echo "<div class=\"navigation\">\n";
  60.       $pages=array();
  61.       $pagenum=ceil($num/$perpage);
  62.       for($i=0;$i<$pagenum-1;$i++)
  63.       {
  64.         $result->data_seek($perpage*$i);
  65.         $row = $result->fetch_object();    
  66.         $pages[$i]=$row->id;
  67.         echo ($i==0?'':'|').($page==$i?' '.$i.' ':' <a href="?page='.$i.'">'.($i+1).'</a> ');        
  68.       }
  69.       echo "</div>\n";
  70.       $_SESSION['pages']=$pages;
  71.       $_SESSION['pagenum']=$pagenum;
  72.     }  
  73.     $result->close();
  74.   }
  75. }else{
  76.   if($_SESSION['pagenum']>0){
  77.     echo "<div class=\"navigation\">\n";
  78.     for($i=0;$i<$_SESSION['pagenum'];$i++){
  79.       echo ($i==0?'':'|').($page==$i?' '.$i.' ':' <a href="?page='.$i.'">'.($i+1).'</a> ');
  80.     }
  81.     echo "</div>\n";
  82.   }
  83. }
  84. // paging model: prev | next
  85. // paging model: first|prev|next|last
  86. // paging model: prev|... 4|5|[6]|7|8 ..|next
  87. $db->close();
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement