如何完成分页实现

时间:2011-07-07 20:41:27

标签: php html css pagination organization

感谢你们中的一些人的大力帮助,我终于在我的第一个分页项目中取得了成功......差不多。

除了一个小故障外,我的脚本运行得非常好。有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> ";
}

提前致谢

1 个答案:

答案 0 :(得分:1)

更改您的

if ($currentpage != $totalpages)) {

if ($currentpage < $totalpages) {

首先,有一个额外的括号,你不应该依赖于完全匹配的上限。

修改

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

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

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