gpt4 book ai didi

php - 分页。为什么只显示一页?

转载 作者:行者123 更新时间:2023-11-29 22:31:35 28 4
gpt4 key购买 nike

我一直在尝试让分页正常工作,并且已经达到了一定程度,但它只显示一页结果。

我的数据库中有 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');
?>

最佳答案

我认为你必须删除该行:

 $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];

您必须选择以上其中一项,但不能同时选择两者:)

关于php - 分页。为什么只显示一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729569/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com