php分页不显示下一条记录

标签 php mysql

抱歉,我之前错误地将这个问题放在了 WordPress 上。我正在处理分页,但我面临一个问题,当我单击“下一步”时,它会增加下一页的查询字符串中的数字,但页面上的数据保持不变,我需要显示接下来的四个记录,因为我每条显示了 4 条记录页。代码看起来不错,但我不知道为什么它不起作用。下面是代码

$page = 0;

if($page == 0){

    $page = "1";

}else{

    // If page is set, let's get it
  echo   $page = $_GET['page'];

}

// Now lets get all messages from your database
$sql = "select * from about_us where sbmenu_id=9991";
$query = mysql_query($sql);

// Lets count all messages
$num = mysql_num_rows($query);

// Lets set how many messages we want to display
$per_page = "4";

// Now we must calculate the last page
$last_page = ceil($num/$per_page);

// And set the first page
$first_page = "1";

// Here we are making the "First page" link
echo "<a href='?page=".$first_page."'>First page</a> ";
echo $page ;
// If page is 1 then remove link from "Previous" word
if($page == $first_page){

    echo "Previous ";

  }else{

    if(!isset($page)){

            echo "Previous";

    }else{

            // But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
            $previous = $page-1;
            echo "<a href='?page=".$previous."'>Previous</a> ";

    }

 }

 // If the page is last page.. lets remove "Next" link
 if($page == $last_page){

    echo "Next ";   

  }else{

    // If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
    if(!isset($page)){

            $next = $first_page+1;
            echo "<a href='?page=".$next."'>Next</a> ";

    }else{

            $next = $page+1;
            echo "<a href='?page=".$next."'>Next</a> ";

    }

 }

 // And now lets add the "Last page" link
 echo "<a href='?page=".$last_page."'>Last page</a>";

 // Math.. It gets us the start number of message that will be displayed
 $start = ($page-1)*$per_page;

 // Now lets set the limit for our query
 $limit = "LIMIT $start, $per_page";

// It's time for getting our messages
$sql = "select * from about_us where sbmenu_id=9991 $limit";
$query = mysql_query($sql);
 ?><table><?php
echo "<br /><br /><tr style='height:70px;'>";

// And lets display our messages
while($row = mysql_fetch_array($query) or die(mysql_error()))
 {....}  

最佳答案

更改前几行,然后尝试..

if(isset($_GET['page']) && $_GET['page'] > 0)        // If page is set, let's get it
{
    echo   $page = $_GET['page'];  
}
else
{
    $page = 1;
}

而不是:-

$page = 0;

if($page == 0){

$page = "1";

}else{

// If page is set, let's get it
 echo   $page = $_GET['page'];

}

关于php分页不显示下一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229822/

相关文章:

php - 基于 Feeds 的应用程序的 PHPSESSID 设计

javascript - 单击按钮打开漂亮的照片(多张图片)

php - 如何使 PHP for 循环倒计时而不是向上倒计时?

mysql - 在mysql中自动验证密码

mysql - 在忽略年份的日期范围之间选择

php - 使用 mysql 和 php 将用户链接到设备

php - PHP 和 NODEjs 之间保存( secret )变量数据的通用文件类型?

php - 哪个最不密集 - 在 PHP 或 MySQL 中计算唯一时间?

MySQL 在连接函数时使用索引

mysql - 有没有一种方法可以在将两个表连接在一起时覆盖它们的值,并在需要时添加列?