在下一页显示while循环结果?

时间:2012-12-14 18:44:16

标签: php mysql

您好我有一个功能,它收集用户添加的帖子列表,并在页面中的while循环中显示它。

我限制了显示的结果数量,即LIMIMT 15,因此它不会在页面上溢出。但我现在想要做的是拥有所有其他结果(无论用户可能添加多少)以显示在下一页上。每个页面只显示15个结果。所以你最终得到第1页,第2页,第3页等。

这是如何实现的?

function get_forum() {
            global $connection;
            global $profile_id;
            $query = "SELECT *
                        FROM ptb_forum
                        WHERE ptb_forum.deleted ='0'
                        ORDER BY ptb_forum.date_added DESC
                        LIMIT 0 , 16";
            $forum_set = mysql_query($query, $connection);
            confirm_query($query, $connection);
            return $forum_set;
        }

<?php
    $forum_set = get_forum();

    ?>
    <br/>
    <h3>Latest Forum Posts</h3>
    <br/> 

    <?php
    if(mysql_num_rows($forum_set) > 0) {
        while ($forum = mysql_fetch_array($forum_set)) {
            $age = days_from_date($forum['date_added']);
            ?>
            <div class="prof-content-box-forum" id="reviews">
             <div class="forum-content">
             <?php echo "{$forum['content']}"; ?>
             </div>
             <div class="message_pic">
             <?php echo "<a href=\"profile.php?id={$forum['from_user_id']}\"><img width=\"50px\" height=\"50px\"  src=\"{$prof_photo}\"></a>";?>

             </div>

             <div class="forum-text">
             <?php echo "Posted by {$forum2['display_name']}"; ?> <?php echo "".$age." days ago"; ?>
          </div>

             </div>

             <? } } ?>

1 个答案:

答案 0 :(得分:1)

get_forum();需要对其所在页面进行分析。

    $page = $_GET['page'];
    $page or $page = 1;
    $offset = ($page * 15) - 15;
    // obviously you need to sanitize this

    "SELECT *
    FROM ptb_forum
    WHERE ptb_forum.deleted ='0'
    ORDER BY ptb_forum.date_added DESC
    LIMIT $offset , 16"

您还需要写出分页,以便用户可以浏览这些页面。 关于php分页的基本谷歌搜索将为您提供许多预制功能。 此示例假定您使用$ _GET var“page”来表示您所在的页面。例如,yoursite.com/forum.php?page=2

此外,我只是将查询本身作为示例,因为有许多方法可以执行此操作,而不使用mysql_函数。

你应该停止使用mysql_函数。 PDOmysqli非常容易实施,并且在整个网站以及整个互联网上都存在大量支持。