简体   繁体   中英

Pagination. Why only one page is showing?

I have been trying to get pagination to work and I have to a certain extent but it only shows one page of results.

I have 18 listings in my database and have it set to show 6 per page.

Can anyone see what the problem is?

<?php include('includes/configsql.php'); 

//include header template
include('layout/header.php'); 

//include navbar template
include('layout/navbar.php'); 


?>

<?php
$con = mysqli_connect($db_hostname,$db_username,$db_password,$db_database);

$sql = "SELECT COUNT(id) FROM basic WHERE status='active'";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);

$rows = $row[0];
$rows = mysqli_num_rows($query);

$page_rows = 6;
$last = ceil($rows/$page_rows);

if($last < 1){
    $last = 1;
}

$pagenum = 1;

if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}

if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT id, name, address, telephone, email, category FROM basic WHERE status='active' ORDER BY name ASC $limit";
$query = mysqli_query($con, $sql);

$textline1 = "Basic Listing (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";

$paginationCtrls = '';
if($last != 1){
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }

    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }
}
$list = '';
while($row = mysqli_fetch_array($query)){
    $id = $row["id"];
    $name = $row["name"];
    $category = $row["category"];
    $list .= '<p><a href="business.php?id='.$id.'">'.$name.' &nbsp;|&nbsp;'.$category.' </a> - Click the link to view this business</p>';
}

mysqli_close($con);
?>


<div id="wrapper">


<div id="bizlist">

 <div id="pagination">
  <h2><?php echo $textline1; ?></h2>
  <p><?php echo $textline2; ?></p>
  <p><?php echo $list; ?></p>
  <div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>

</div>



<?php 
//include footer template
include('layout/footer.php'); 
?>

I think you have to remove the line:

 $rows = mysqli_num_rows($query);

It is in beginning of the script.

Another way is if you modify the first line like this:

$sql = "SELECT * FROM basic WHERE status='active'";

And remove the next 3 rows

$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];

You have to choice one of above, but not both :)

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