dqk94069 2016-02-18 06:43
浏览 14
已采纳

我想明智地显示数据(PAGINATION)

I have one field in admin panel name PostDisplay,when i enter 3 in postDisplay then on post page (frontside) it will display 3 post, my question is when i enter 20 in postDisplay it display 20 post but i want a pagination on page thus page 1 display 5 post 2nd page next 5 and so on, this is my php page which get data and display post.

<?php
    include_once ('class/user_posts.php');
    $user_posts = new user_posts;
    $slug = "post";
    $svs = $user_posts->select(" where A.type='" . $slug . "' ", "", "0", "$postcount");
    $blogArray = array();
    if (mysql_num_rows($svs) != 0) {
        while ($row = mysql_fetch_array($svs)) {
            $blogArray[] = $row;
        }
    }
?>
<div class="center margin-bottom-lg">
    <div class="container"> 
        <div class="row padding-bottom-lg">
            <div class="col-md-12">
                <div class="gray-box">
                    <h1 class="header">
                       <?php echo $user_options->getvalue('blogtitle', 'Blog...'); ?>
                    </h1>
                <div class="row our-services margin-bottom-lg padding-lg">
<?php
   header('Cotent-type: text/plain'); // Just here for formatting
   foreach ($blogArray as $array) {
       echo '<div class="col-md-4">';
       echo '<div class="row">';
       echo '<div class="col-xs-12">';
       echo '<a class="title-link" href="#">';
       echo '<h3>' . $array['slug'] . '</h3>';
       echo '</a>';
       echo '<p>' . $array['description'] . '</p>';
       echo '<p class=""><a href="#" class="btn-link">Read More...</a></p>';
       echo '</div>';
       echo '</div>';
       echo '</div>';
    }
?>
                    </div>
                </div>
            </div>
        </div>
    </div> 
</div>
  • 写回答

1条回答 默认 最新

  • duanchuonong5370 2016-03-30 21:11
    关注

    It seems $svs is the variable that contains the data you want to paginate but we don't have enough info like, ok, PostDisplay is the variable that manipulate how many posts will show up on your page but your code doesn't mention it once. So I will go with global advices on how to paginate:

    To paginate, you need to use the LIMIT clause in the SQL statement that will search your data. LIMIT takes to arguments, "the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return". (From https://dev.mysql.com/doc/refman/5.5/en/select.html)
    So, let says postDisplay is equal to 3, by using LIMIT 0, 3 (<=postDisplay), your SELECT statement will return the first three 3 results of your statement. With postDisplay=20 (LIMIT 0, 20), first 20 results.
    Now, you have 20 user posts and you want to make pagination to render 4 pages with 5 posts each. You need to set postDisplay to 5 and use another variable to calculate the first argument of the LIMIT clause, because, on page 1, you will display results from 0 to 4, page 2, 5 to 9, page 3, 10 to 14, last page 15 to 19. That first argument will be 0 then 5 > 10 > 15. It is where it starts.

    To calculate it, you need to know on what page you are, if it's page 1, 2, 3 or 4. So you set a $_GET['page'] and with it you will calculate based on the postDisplay, if your first argument will be 0, 5, 10 or 15.
    You also need to make a statement to know how many user posts you have in your database to start the calculation on how to cut that number in slices.

    //here you get how many user posts total there is
    $req_nbr_user_posts=mysqli_query($yourdbconnexion,"SELECT COUNT(*) AS nbr FROM user_posts");
    $nbr_user_posts=mysqli_fetch_assoc($req_nbr_user_posts);
    
    $postDisplay=5;
    $current_page=$_GET['page'];
    
    //here you have the total of pages for your pagination with $pages_total
    $number_of_pages=floor($nbr_user_posts['nbr']/$postDisplay);
    if($nbr_user_posts['nbr']%$postDisplay==0) $rest=0;
    else $rest=1;
    $pages_total=$number_of_pages+$rest;
    
    //here is how to calculate that first argument of the LIMIT clause
    $first_argument=($current_page-1)*$postDisplay;
    
    $what_to_display=mysqli_query($yourdbconnexion,"SELECT * FROM user_posts LIMIT ".$first_argument.", ".$postDisplay);
    

    That's it!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?