gpt4 book ai didi

php - 如何完成分页实现

转载 作者:行者123 更新时间:2023-11-28 02:13:45 26 4
gpt4 key购买 nike

感谢你们中的一些人的大量帮助,我终于在我的第一个分页元素中取得了成功……差不多吧。

除了一个小问题,我的脚本工作得很好。共有40"行"页,当没有更多"行"显示时,分页继续分页空白页。

更具体一点。第 13 页是最后一页,上面有行,但您仍然可以点击下一步并转到空白页。

解决方案很明显 - 如果没有剩余行,则让“下一步”按钮消失,但出于某种原因,我尝试过的所有 if 语句都没有这样做。

这里是查询:

$rowsperpage = 40; // THERE ARE 40 AIRWAVES PER PAGE
$currentpage = (int)$_GET['currentpage']; // This is getting which "page" the user wants to see, and putting it in $currentpage

if ($currentpage > 0) { // If $currentpage is greater than 0, we'll need to compensate by subtracting 1, so that we pull the correct set of results. Otherwise, we'll just start at 0 (see the else)
$offset = ($currentpage - 1) * $rowsperpage;
}
else {
$offset = 0;
}

$query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC LIMIT $offset, $rowsperpage";
$request = mysql_query($query, $connection);
$counter = 0;

while ($result = mysql_fetch_array($request)) {

这是分页链接:

// find out how many rows are in the table

$query = "SELECT COUNT(*) FROM `CysticAirwaves`";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page

$rowsperpage = 40;

// find out total pages

$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {

// cast var as int

$currentpage = (int)$_GET['currentpage'];
}
else {

// default page num

$currentpage = 1;
} // end if

// if current page is greater than total pages...

if ($currentpage > $totalpages) {

// set current page to last page

$currentpage = $totalpages;
} // end if

// if current page is less than first page...

if ($currentpage < 1) {

// set current page to first page

$currentpage = 1;
} // end if

// the offset of the list, based on current page

$offset = ($currentpage + 1) * $rowsperpage;

// while there are rows to be fetched...

while ($list = mysql_fetch_assoc($result)) {

// echo data

echo $list['id'] . " : " . $list['number'] . "<br />";
} // end while
/****** build the pagination links ******/

// range of num links to show

$range = 3;

// if not on page 1, don't show back links

if ($currentpage > 1) {

// show << link to go back to page 1

$prevpage = $currentpage - 1;
echo "<div id='all_page_turn'>
<ul>
<li class='PreviousPageBlog round_10px'>
<a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$prevpage'>Previous</a>
</li>
<ul>
</div>";
} // end if

// if not on last page, show forward and last page links

if ($currentpage != $totalpages)) {

// get next page

$nextpage = $currentpage + 1;

// echo forward link for next page

echo " <div id='all_page_turn'>
<ul>
<li class='PreviousPageBlog round_10px'>
<a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$nextpage'>Next</a>
</li>
</ul>
</div> ";
}

提前致谢

最佳答案

改变你的

if ($currentpage != $totalpages)) {

if ($currentpage < $totalpages) {

首先,有一个额外的括号,您永远不应该依赖于精确匹配上限。

编辑:

我想我看到了问题所在。在执行 COUNT 时,您需要使用与实际选择查询中相同的 WHERE 子句:

$query = "SELECT COUNT(*) FROM `CysticAirwaves` " .
"WHERE `FromUserID` = `ToUserID` AND `status` = 'active'";

否则,您将获得表格中所有 行的计数,即使您没有显示的行也是如此。

关于php - 如何完成分页实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6616910/

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