Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 29th, 2011  |  syntax: PHP  |  size: 2.41 KB  |  views: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. include 'includes/connection.php';  
  3. $per_page = 8;
  4. $pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
  5. $pages = ceil(mysql_result($pages_query, 0) / $per_page);
  6.  
  7. $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
  8. $start = ($page - 1) * $per_page;
  9.  
  10. $query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
  11. while ($query_row = mysql_fetch_assoc($query))
  12. {
  13.         echo '<p>' . $query_row['name'] . '</p>';
  14. }
  15.  
  16. // If the requested page is less than 1 or more than the total number of pages
  17. // redirect to the first page
  18. if($pages < 1 || $page > $pages)
  19. {
  20.         header('Location: ?page=1');
  21.         // end execution of the rest of this script
  22.         // it will restart execution after redirection
  23.         exit;  
  24. }
  25. // If more than one page, show pagination links
  26. if($pages > 1)
  27. {
  28.     $html = array();
  29.     $html[] = '<strong>';
  30.         // if you're on a page greater than 1, show a previous link
  31.         $html[] = (($page > 1) ? '<a href="?page=' . ($page - 1) . '">Previous</a> ' : 'Previous');
  32.         // First page link
  33.     $pageFirst = '<a href="?page=1">1</a>';
  34.         $html[] = (($page == 1) ? "</strong>{$pageFirst}<strong>" : $pageFirst);
  35.    
  36.     if ($pages > 6)
  37.         {
  38.                 $start_cnt = min(max(1, $page - (6 - 1)), $pages - 6);
  39.                 $end_cnt = max(min($pages, $page + 4), 8);
  40.  
  41.                 $html[] = ($start_cnt > 1) ? '...' : ' ';
  42.  
  43.                 for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
  44.                 {
  45.                         $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>';
  46.                         if ($i < $end_cnt - 1)
  47.                         {
  48.                                 $html[] = ' ';
  49.                         }
  50.                 }
  51.  
  52.                 $html []= ($end_cnt < $pages) ? '...' : ' ';
  53.         }
  54.         else
  55.         {
  56.                 $html[] = ' ';
  57.  
  58.                 for ($i = 2; $i < $pages; $i++)
  59.                 {
  60.                         $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>';
  61.                         if ($i < $pages)
  62.                         {
  63.                                 $html[] = ' ';
  64.                         }
  65.                 }
  66.         }
  67.     // last page link
  68.     $pageLast = '<a href="?page=' . $pages . '">' . $pages . '</a>';
  69.         $html[] = (($page == $pages) ? "</strong>{$pageLast}<strong>" : $pageLast);
  70.         // Show next page link if you're on a page less than the total number of pages
  71.         $html[] = ($page < $pages) ? ' <a href="?page=' . ($page + 1) . '">Next</a>' : 'Next';
  72.         // If you're not on the last page, show a next link
  73.         $html[] = '</strong>';
  74. }
  75. else
  76. {
  77.     // show page number 1, no link.
  78.     $html[] = '<strong>1</strong>';
  79. }
  80. echo implode('', $html);
  81.  
  82.