简体   繁体   English

PHP分页问题,​​给出空白页

[英]PHP pagination problem, gives blank page

Below is a code for my pagination. 以下是我的分页代码。 The problem that I am having is that the page count is displayed correctly but when I click on page 2 onwards I just get a blank page. 我遇到的问题是页数显示正确,但是当我单击第2页以上时,我只会得到一个空白页。

        <?php   
if (isset($_POST['edit'])) {
            if (empty($_GET['page'])) {
                $page=0;
            }
            else {$page = (int)$_GET['page'];}


         if ($page == 0){ $page = 1;}

          if (ob_get_level() == 0) ob_start();


       $per_page = 10;
        $p = ($page - 1) * $per_page;


        $sql="select * from tba where word='$word' order by id DESC limit ".$p.",".$per_page;
        $result=mysql_query($sql) or die(mysql_error());



        while ($row=mysql_fetch_array($result)) {
            $id=$row['id'];
            $word=$row['word'];
            $pr=$row['pr'];


            if ($pr==0) {


            }
    else {
      ?>
        <td><span class="style5"><?php echo $id; ?> </span></td>
        <td><span class="style5"><?php echo $word?></span></td>
    <?php

            }
                    $pages = floor($total / $per_page) + ($total%$per_page>0?1:0);
    ?>
    <center>
    <?php
        }
    for ($i=1;$i<=$pages;$i++) {
      print "<a href='?page=".$i."'>".$i."</a> ";

    }

     echo "<br>You are in page ".$_GET['page']; 
} 

can some one please tell me what the problem is? 有人可以告诉我问题是什么吗?

It's time to learn debugging. 现在该学习调试了。

As a matter of fact, we can only guess what is going wrong. 事实上,我们只能猜测出了什么问题。
While it is only programmer oneself who can tell it for sure. 虽然只有程序员自己才能确定。 It's their job and duty. 这是他们的工作和职责。
The art of finding what is going wrong is called debugging. 发现问题所在的技巧称为调试。

To do it, you have to check every operation result. 为此,您必须检查每个操作结果。
For example, does your query return any results? 例如,您的查询是否返回任何结果?
If not - why? 如果没有-为什么? Where does that $word variable come from? 该$ word变量从何而来? But on the second page? 但是在第二页上?

You have to pass all required data to other pages as well as $page variable. 您必须将所有必需的数据以及$ page变量传递给其他页面。

It looks like the $pages = floor... line is inside the while loop, which is the first problem. 看起来$ pages = floor ...行在while循环内,这是第一个问题。 Also, $total is never being set. 而且,永远不会设置$ total。

The variable $total is never set making the total pages count incorrect 永远不会设置变量$total使总页数不正确

You need to fetch first the total number of rows with a query similar to: 您需要先使用类似于以下内容的查询来获取总行数:

$sql="SELECT COUNT(id) AS rows FROM tba where word='$word'";

Instead of doing the "floor" operation and adding "1" when has "other page" you can use "ceil" 您可以使用“ ceil”代替“ floor”操作并在有“ other page”时添加“ 1”

Instead of: 代替:

$pages = floor($total / $per_page) + ($total%$per_page>0?1:0);

You can use: 您可以使用:

$pages = ceil($total / $per_page);

好吧,整个输出在if语句中,并且您没有发布POST参数($_POST['edit']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM