简体   繁体   中英

PHP Pagination not returning expected results

I am looking for a bit of guidance as to where I am going wrong with this pagination attempt, below is an example of my index.php file - upon loading index.php everything appears to work as expected, I get my results from the database, it is restricted to the set limit and it has the correct number of links at the bottom, the problem is when I click the link to go to index.php?page=1 or index.php?page=2 etc it throws me an error suggesting my $query is 0.

This is a project I am using to self teach myself the language - so any general tips are also appreciated.

Thanks for your time.

<?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'); ?>

Take a look at this line of code:

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

Do you see the error? Your if statement will evaluate to true every time the page is NOT equal to 1. Remove the '!' and put one more '|'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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