Advertisement
Guest User

Untitled

a guest
Apr 16th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1. <?php
  2.         //http://www.phpbuilder.com/board/showthread.php?t=10353797
  3.         $host = "localhost";
  4.         $user = "root";
  5.         $pass = "";
  6.         $db = "project";
  7.  
  8.     // open connection
  9.     $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
  10.        
  11.     // select database
  12.     mysql_select_db($db) or die ("Unable to select database!");
  13.  
  14.     // how many rows to show per page
  15.     $rowsPerPage = 1;
  16.  
  17.     // by default we show first page
  18.     $page_num = 1;
  19.  
  20.     // if $_GET['page'] defined, use it as page number, $_GET gets the page number out of the url
  21.     //set by the $page_pagination below
  22.     if(isset($_GET['page'])){$page_num = $_GET['page'];}
  23.  
  24.     //the point to start for the limit query
  25.     $offset = $page_num;
  26.  
  27.     // Zero is an incorrect page, so switch the zero with 1, mainly because it will cause an error with the SQL
  28.     if($page_num == 0) {$page_num = 1;}
  29.  
  30.     // counting the offset
  31.     $sql = ("SELECT * FROM comments LIMIT $offset, $rowsPerPage");
  32.     $res = mysql_query($sql) or die(mysql_error());
  33.  
  34.     // how many rows we have in database
  35.     $sql2  = "SELECT COUNT(comment_id) AS numrows FROM comments";
  36.     $res2  = mysql_query($sql2) or die(mysql_error());
  37.     $row2  = mysql_fetch_array($res2);
  38.     $numrows = $row2['numrows'];
  39.  
  40.     // print the random numbers
  41.     while($row = mysql_fetch_array($res))
  42.     {
  43.         //Echo out your table contents here.
  44.  
  45.         echo $row[1].'<BR>';
  46.         echo $row[2].'<BR>';
  47.         echo '<BR>';
  48.     }
  49.  
  50.     // how many pages we have when using paging?
  51.     $numofpages = ceil($numrows/$rowsPerPage);
  52.  
  53.     // print the link to access each page
  54.     $self = "events.php?";
  55.  
  56.     if ($numofpages > '1' ) {
  57.  
  58.                 $range = 10; //set this to what ever range you want to show in the pagination link
  59.                 $range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
  60.                 $range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
  61.                 $page_min = $page_num- $range_min;
  62.                 $page_max = $page_num+ $range_max;
  63.  
  64.                 $page_min = ($page_min < 1) ? 1 : $page_min;
  65.                 $page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
  66.                 if ($page_max > $numofpages) {
  67.                     $page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
  68.                     $page_max = $numofpages;
  69.                 }
  70.  
  71.                 $page_min = ($page_min < 1) ? 1 : $page_min;
  72.  
  73.                 //$page_content .= '<p class="menuPage">';
  74.  
  75.                 if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) {
  76.                     $page_pagination .= '<a class="num"  title="First" href="'.$self.'page=1">&lt;</a> ';
  77.                 }
  78.  
  79.                 if ($page_num != 1) {
  80.                     $page_pagination .= '<a class="num" href="'.$self.'page='.($page_num-1). '">Previous</a> ';
  81.                 }
  82.  
  83.                 for ($i = $page_min;$i <= $page_max;$i++) {
  84.                     if ($i == $page_num)
  85.                     $page_pagination .= '<span class="num"><strong>' . $i . '</strong></span> ';
  86.                     else
  87.                     $page_pagination.= '<a class="num" href="'.$self.'page='.$i. '">'.$i.'</a> ';
  88.                 }
  89.  
  90.                 if ($page_num < $numofpages) {
  91.                     $page_pagination.= ' <a class="num" href="'.$self.'page='.($page_num + 1) . '">Next</a>';
  92.                 }
  93.  
  94.  
  95.                 if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) {
  96.                     $page_pagination .= ' <a class="num" title="Last" href="'.$self.'page='.$numofpages. '">&gt;</a> ';
  97.                 }
  98.                
  99.                 //$page['PAGINATION'] ='<p id="pagination">'.$page_pagination.'</p>';
  100.             }//end if more than 1 page
  101.  
  102.     echo $page_pagination.'<BR><BR>';
  103.  
  104.     echo 'Number of results - '.$numrows ;
  105.     echo ' and Number of pages   - '.$numofpages.'<BR><BR>';
  106.  
  107.     // Free resultset
  108.     mysql_free_result($res);
  109.  
  110.  
  111.  
  112. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement