PHP分页不起作用

时间:2014-07-08 07:49:05

标签: php pagination

我的下面的代码是一个php分页,它正在加载正确的结果,但问题是,当我点击下一页时,结果应该是下一个结果,但它是相同的,它不会改变,我不明白是什么是错的?

<html>
    <head>
        <title>Untitled Document</title>
    </head>
    <body>
    <?php 
    // Connects to your Database 
    mysql_connect("localhost", "username", "password") or die(mysql_error()); 
    mysql_select_db("dbname") or die(mysql_error()); 

    //This checks to see if there is a page number. If not, it will set it to page 1 
    $pagenum = $_GET['pagenum'];
      if (!(isset($pagenum))) { 
      $pagenum = 1; 
     } 

    //Here we count the number of results 

    //Edit $data to be your query 
    $data = mysql_query("SELECT * FROM information") or die(mysql_error()); 
    $rows = mysql_num_rows($data); 

    //This is the number of results displayed per page 
    $page_rows = 3; 

    //This tells us the page number of our last page 
    $last = ceil($rows/$page_rows); 

    //this makes sure the page number isn't below one, or more than our maximum pages 
    if ($pagenum < 1) { 
        $pagenum = 1; 
    } else if ($pagenum > $last) { 
        $pagenum = $last; 
    } 

    //This sets the range to display in our query 
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;       

    $data_p = mysql_query("SELECT * FROM information $max") or die(mysql_error()); 

    //This is where you display your query results
    while($info = mysql_fetch_array( $data_p )) { 
        print $info['id'];
        echo "<br>"; 
        print $info['phone'];
        echo "<br>"; 
        print $info['address']; 
        echo "<br>";
        echo "<hr>";
    } 

    echo "<p>";

    // This shows the user what page they are on, and the total number of pages
    echo " --Page $pagenum of $last-- <p>";

    if ($pagenum == 1) {
    } else {
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
        echo " ";

        $previous = $pagenum-1;

        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    } 


    //just a spacer
    echo " ---- ";


    //This does the same as above, only checking if we are on the last page, and then  generating the Next and Last links
    if ($pagenum == $last) {
    } else {
        $next = $pagenum+1;

        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
        echo " ";
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    } 
    ?> 
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

<?php
// Database have two Columns id and email
require_once 'profile/include/data_base.php';
$limit=5; // How many Number of record will fetch at one page
$link="http://$server_name/rishi/pagination1.php";
?>
<!-- HTML Parts-->
<html>
 <head> <title>Pagination Example</title>
 </head>
 <body>
 <center>Pagination Example in PHP</center>
   <?php
    if(isset($_GET["page_id"])){
        $page_id=$_GET["page_id"];
    }
    else{
      // By Default First Page will Display
        $page_id=1; 
    }


// From Starting Content on Page
$from_page=($page_id-1) * $limit;

// To get the Total Number of Columns in Table
$total_data=$con->query("select * from r_register");

//Total numbers of Tabs that will display below
$total_tabs=ceil($total_data->num_rows/$limit);

$all_data=$con->query("select * from r_register LIMIT ".$from_page.",".$limit);
if($all_data->num_rows > 0)
    {
        while($row=$all_data->fetch_assoc()){
            echo $row["id"] ." ".$row["email"]."<br>";
    }
}


?>

<div class="pagination">
  <?php
  $num=1;
   // To take Previous Page or to display '<'
  if($page_id >1)
  {
    $minus_one=$page_id-1;
     echo '<a href="'.$link.'?page_id='.$minus_one.'" class="a_link ">&lt</a>';
  }
  // Starting two page number will always be there.
if($page_id >1){
 echo '<a href="'.$link.'?page_id=1" class="a_link">1</a>'; 
}
if($page_id >2){
 echo '<a href="'.$link.'?page_id=2" class="a_link">2</a>'; 
}
// .. will Display
if($page_id >4)
{
  echo '<a href="'.$link.'?page_id='.($page_id-2).'" class="a_link">..</a>';
}

// One  Previous  From Current Page
if($page_id >3){
    $current_preivous=$page_id-1;

    echo '<a href="'.$link.'?page_id='.$current_preivous.'" class="a_link">'.$current_preivous.'</a>';  
}


    while($num <=$total_tabs){
       if($page_id == $num){
      // To make css Change On Current page
        echo '<a href="'.$link.'?page_id='.$num.'" class="a_link a_active">'.$num.'</a>';
       }
        $num++;
     }



if($total_tabs-$page_id >=3)
{
  // Next Page from Current Page
    $current_next=$page_id+1;
    echo '<a href="'.$link.'?page_id='.$current_next.'" class="a_link">'.$current_next.'</a>';
}

//one Previous constant value

    if($page_id<$total_tabs-3)
     {
      $preious_end=$total_tabs-2;
      echo '<a href="'.$link.'?page_id='.$preious_end.'"class="a_link">..</a>';
     }

     if($page_id<$total_tabs-1)
     {
        $preious_end=$total_tabs-1;
        echo '<a href="'.$link.'?page_id='.$preious_end.'" class="a_link">'.$preious_end.'</a>';
     }

     if($page_id < $total_tabs){
     echo '<a href="'.$link.'?page_id='.$total_tabs.'" class="a_link">'.$total_tabs.'</a>';
     }


  //To Display '>'
     if($page_id < $total_tabs)
  {
    $plus_one=$page_id+1;
    echo '<a href="'.$link.'?page_id='.$plus_one.'" class="a_link">&gt</a>';
  }
 ?>
</div>
 </body>

 <style>
 .pagination{
    background-color: #039a9a;
    font-size: 30px;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
 }
 .a_link{
    text-decoration: none;
    color: red;
    padding: 5px;
 }
 .a_active{
    color: white;
 }
 </style>
</html>

答案 1 :(得分:0)

$ pagenum变量中没有值 设置$pagenum = $_GET['pagenum'];它将起作用

$pagenum = $_GET['pagenum'];
if (!(isset($pagenum))) { 
   $pagenum = 1; 
 }