如何在分页项目中实现最后的步骤


How to implement final steps in pagination project?

我正在对我的站点进行第一次分页实现。我觉得我很接近,只是混淆了我的逻辑,或者误解了一些变量的位置或实际值。

这是代码:

                    $pgsize_wave = 40;
                    $pg_wave = (is_numeric($_GET["p"]) ? $_GET["p"] : 1);
                    $start = ($pg_wave-1)*$pgsize_wave;
                    $waves = mysql_query("SELECT * FROM `CysticAirwaves` LIMIT $start, $pgsize_wave");
                    $waves_total = mysql_query("SELECT COUNT(1) FROM `CysticAirwaves`");
                    $waves_total = mysql_fetch_row($waves_total);
                    $waves_total = $waves_total[0];
                    $max_pages = $waves_total / $pgsize_wave;
                    $max_pages = ceil($waves_total/$pgsize_waves);

                    ?>

                    <div id="all_page_turn">
                             <ul>
                                <?php if($waves_total > 40 && $pg_wave >= 40) { ?>
                                 <li class="PreviousPageBlog round_10px">
                                     <a href="Airwave_build.php?id=<?php echo $pg_wave - 40); ?>">Previous Page</a>
                                 </li>
                                 <?php } ?>
                                 <?php if($waves_total > 40 && $pg_wave < ($waves_total-40)) { ?>
                                 <li class="NextPageBlog round_10px">
                                     <a href="Airwave_build.php?id=<?php echo ($pg_wave + 40); ?>">Next Page</a>
                                 </li>
                                 <?php } ?>
                             </ul>
                        </div>

为了澄清任何困惑,无线电波是用户的帖子,我想限制每页40条。如果它在这里有帮助的话,那就是在同一页面上拉一个电波的查询:

$query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC";
            $request = mysql_query($query,$connection);
            $counter = 0;
            while($result = mysql_fetch_array($request)) {
            $replies_q = "SELECT COUNT(`id`) FROM `CysticAirwaves_replies` WHERE `AirwaveID` = '" . $result['id'] . "' && `status` = 'active'";
            $request2 = mysql_query($replies_q,$connection);
            $replies = mysql_fetch_array($request2);
            $replies_num = $replies['COUNT(`id`)'];
            $counter++;
            $waver = new User($result['FromUserID']);

提前非常感谢。

这是伪代码,但有望解释逻辑。

$total        = SELECT COUNT(*) FROM `waves`
$currentPage  = 1  // changes through page parameter in URL
$wavesPerPage = 40
$totalPages   = ceil($total / $wavesPerPage)
$offset       = ($wavesPerPage * ($currentPage - 1)) + 1
$waves = mysql_query("SELECT * FROM `waves` LIMIT $offset, $wavesPerPage");
while ($row = mysql_fetch_assoc($waves)) {
    echo $row['wave']
    …
}

输出分页链接:

if ($totalPages > 1) {
    if ($currentPage > 1) {
        printf('<a href="waves.php?page=%u">Previous</a>', $currentPage - 1);
    }
    for ($i = 1; $i <= $totalPages; $i++) {
        $class = ($i == $currentPage) ? 'selected' : null;
        printf('<a href="waves.php?page=%1$u" class="%2$s">Page %1$u</a>', $i, $class);
    }
    if ($currentPage < $totalPages) {
        printf('<a href="waves.php?page=%u">Next</a>', $currentPage + 1);
    }
}