如何实现分页项目的最终步骤?

时间:2011-06-09 01:46:03

标签: php css pagination

我正在对我的网站进行第一次分页实施。我觉得我很亲密,只是混淆了我的逻辑或误解了一些变量的位置或实际价值。

以下是代码:

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

为了澄清任何混淆,airwave是来自用户的帖子,我想限制每页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']);

提前非常感谢。

1 个答案:

答案 0 :(得分:2)

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

$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);
    }
}