[Pagination] Navigate Entries per page UPDATED

A Simple way to display entries per page with limits to group numbers so they do not get too big and styles for the navigation bar
Live example is here

  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Numbering Sample</title>
  7. <style>
  8. #numbers {
  9.         font:normal 100%/200% Georgia, "Times New Roman", Times, serif;
  10.         padding:2px 20px;
  11.         clear:both;
  12.         text-align:center;
  13. }
  14. #numbers a {
  15.         border:1px #0066FF solid;
  16.         padding:3px 4px;
  17.         margin:2px;
  18.         text-decoration:none;
  19. }
  20. #numbers a:hover, #numbers u {
  21.         border:1px #0066FF solid;
  22.         padding:3px 4px;
  23.         margin:2px;
  24.         background:#000099;
  25.         color:#FFF;
  26. }
  27. </style>
  28. </head>
  29. <body><h1>Example of numbering entries in PHP</h1>
  30. <p>
  31. Please get the code <a href="http://gadelkareem.com/2006/10/14/entries-per-page/">here</a>
  32.  
  33.  
  34. <?php
  35.  
  36. #getting Gadelkareem feed as a sample array
  37. $parser = xml_parser_create();    
  38. xml_parse_into_struct($parser,file_get_contents('http://gadelkareem.com/feed') , $vals, $index);
  39. xml_parser_free($parser);
  40.  
  41.  
  42.  
  43. $limit = 3; #number of entries per page
  44. $st = 1; #how many number we skip before putting '….';
  45. $max = count($index['TITLE']); #counting the array we're displaying
  46.  
  47.  
  48. ###here goes the page
  49. $pg = isset($_GET['pg']) ? $_GET['pg'] : 1;
  50. $nlimit= $max > $pg*$limit ? $pg*$limit : $max;
  51.  
  52.  
  53. #printing feed
  54. for($i=(($pg-1)*$limit); $i<($nlimit+1);$i++){
  55.         echo "<li><a href=\"{$vals[$index['LINK'][$i]]['value']}\">{$vals[$index['TITLE'][$i]]['value']}</a></li>";
  56. }
  57.  
  58.  
  59. #the numbers
  60. $nav = '<div id="numbers">';
  61. $tp = ceil($max/$limit);
  62. $st++;
  63. for($x=1; $x<($tp+1); $x++){
  64.         if($tp > ($st*2)){
  65.                 if(($x==$st+1 && $pg > ($st*2))){
  66.                         $x = $pg-$st;
  67.                         $nav .= '….';
  68.                 }elseif($x==($pg+$st) && ($tp-$pg) > ($st*2)){
  69.                         $x = $tp-$st;
  70.                         $nav .= '….';
  71.                 }
  72.         }
  73.         $nav .= $x==$pg ? "<u>{$x}</u>" :"<a href=\"{$_SERVER['PHP_SELF']}?pg={$x}\" >{$x}</a>";
  74. }
  75. $nav .= '</div>';
  76.  
  77. echo $nav
  78. ?>
  79. </p>
  80. </body>
  81. </html>
  82.  
  83.  
  84.  

Tags :

This entry was posted on Saturday, October 14th, 2006 at 12:42 am and is filed under Solutions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

 

Leave a Reply


 Top