PHP分页未返回预期结果

时间:2018-03-18 12:50:50

标签: php mysql pagination

我正在寻找关于这个分页尝试我出错的地方的一些指导,下面是我的index.php文件的一个例子 - 在加载index.php时,一切看起来都按预期工作,我得到了我的结果从数据库,它被限制在设置限制,并在底部有正确的链接数,问题是当我点击链接转到index.php?page = 1或index.php?page = 2等它会抛出一个错误,提示我的$ query是0。

这是我用来自学自语言的项目 - 所以任何一般的提示也会受到赞赏。

感谢您的时间。

<?php
error_reporting(E_ERROR);
require_once 'core/init.php';
$page_index = "index";

$output = NULL;


$page=$_GET["page"]; // get page ID
$page_limit = 8; // set limit variable to restrict data returned
if($page == "" | !$page=="1"){ // if page is 1 or first time loading set 
starting variable for pagination to 0
$page1=0;
} else {
$page1 = ($pages*$page_limit)-$page_limit; // adjust limit variable 
}
if(Session::exists('home')) {
echo '<p>' . Session::flash('home') . '</p>';
}

include('config.php');
include('header.php'); 

$query=$conn->query("SELECT * FROM `rif_listings` LIMIT $page1,$page_limit");   
if($query->num_rows > 0) {
    while($rows = $query->fetch_assoc())
{
    $prop_id = $rows['auto_id'];
    $prop_postcode = $rows['prop_pc'];
    $prop_address= $rows['prop_add1'];
    $prop_town= $rows['prop_add2'];
    $prop_description = $rows['prop_desc'];
    $prop_description = substr($prop_description, 0, 90);
    $prop_rent= $rows['prop_rent'];
    $prop_image = $rows['prop_imagethumb'];
    $prop_postcode = strtoupper($prop_postcode);

    $output .= "<div id='product_display'>
                <a href='properties.php?id=$prop_id' target='_self'>
                <img class='default_thumbnail' src='core.includes/core.imgbin/listings/$prop_id/$prop_image' /> 
                <h5>Post Code: $prop_postcode</h5>
                <b>$prop_address,</b><br /><b>$prop_town</b><br /><br />
                <span class='description'>$prop_description...</span><br /><br />
                <span class='price'>&pound;$prop_rent pcm</span><br /><br />
                <input class='basket_button' type='submit' value='View Listing' /><br /><br />
                <span>Unique ID: $prop_id</span>
                </a>
                </div>";
}
} else {
$output = "Whoops! Something went wrong, please try again.<br />If the problem persists please contact the Administrator.";
}
?>


<?php include('nav.php'); ?>

<?php include('sidebar.php'); ?>

<section id="core_content">
<?php       
    //page pagination 
    $query=$conn->query("SELECT * FROM `rif_listings`");
    $count = $query->num_rows;
    $pages = $count/$page_limit;
    $pages = ceil($pages);
    // echo out result to check it is getting correct output
    echo $pages . " pages found";
    echo ("<br />");

    // echo out db data
    echo $output;
    echo ("<br />");

    // create links to paginate through data
    for($a=1;$a<=$pages;$a++) {
        ?> <a href="index.php?page=<?php echo $a; ?>"> <?php echo $a. " "; ?></a> <?php

    }
    include('banner.php');
?>
</section>

<?php include('footer.php'); ?>

1 个答案:

答案 0 :(得分:0)

看一下这行代码:

if($page == "" | !$page=="1")

你看到错误吗?每当页面 NOT 等于1时,您的if语句将评估为true。删除&#39;!&#39;并再放一个&#39; |&#39;。