分页。为什么只显示一个页面?

时间:2015-04-19 12:00:47

标签: php mysql pagination

我一直试图让分页工作,我必须在一定程度上,但它只显示一页结果。

我的数据库中有18个列表,并设置为每页显示6个。

任何人都可以看到问题所在吗?

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

1 个答案:

答案 0 :(得分:0)

我认为你必须删除该行:

 $rows = mysqli_num_rows($query);

这是在剧本的开头。

另一种方法是修改第一行,如下所示:

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

并删除接下来的3行

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

你必须选择上述之一,但不能同时选择两者:)