如何完成分页实现



感谢你们中的一些人的巨大帮助,我终于成功地完成了我的第一个分页项目…几乎。

我的脚本工作得很好,除了一个小问题。页面有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> ";
}

Thanks in advance

修改

if ($currentpage != $totalpages)) {

if ($currentpage < $totalpages) {

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

编辑:

我想我看到问题了。在执行COUNT时,您需要使用与实际选择查询中相同的WHERE子句:
$query = "SELECT COUNT(*) FROM `CysticAirwaves` " .
         "WHERE `FromUserID` = `ToUserID` AND `status` = 'active'";

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

最新更新