开发者

How to implement final steps in pagination project?

开发者 https://www.devze.com 2023-03-11 19:56 出处:网络
I am doing my first pagination implementation to my site. I feel like I am very close and just confusing my logic or mistaking the placement or actual value of some variables.

I am doing my first pagination implementation to my site. I feel like I am very close and just confusing my logic or mistaking the placement or actual value of some variables.

Here is the code:

                    $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=<?p开发者_运维百科hp 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>

To clarify any confusion, an airwave is a post from a user and I want to limit 40 per page. If it helps here is the query that pulls an airwave on the same page:

$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']);

Thanks so much in advance.


This is pseudo code but hopefully explains the logic.

$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']
    …
}

To output the pagination links:

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

精彩评论

暂无评论...
验证码 换一张
取 消