2012-12-14 44 views
0

嗨,我有一個函數,收集用戶添加的帖子列表,並在頁面中的while循環中顯示它。在下一頁顯示while循環結果?

我已經限制了diplayed結果的數量,即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> 

      <? } } ?> 
+2

在查詢中使用'LIMIT n,m'子句,在下一頁增加'n'運行。谷歌的'分頁'(這就是所謂的)。 – Wrikken

+0

更新您的帖子。我們需要看看get_forum()函數 –

+0

我會寫如LIMIT 15 AND LIMIT n,m嗎? –

回答

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很容易實現,並且在整個網站以及基本上整個互聯網上都存在大量的支持。