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

Tags : 0066FF counting FFF getting here how number numbers printing the

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