2014-09-10 170 views
0

尝试从我的PhpBB安装中显示六个最新的帖子。我很满意它是如何工作的,但它显示了同一个(最近的)帖子的六个副本,而不是大小独特的最新帖子。显示六个最新的PhpBB帖子,而不是六个最新帖子的副本

只是为了确认,我在论坛上有7个帖子。

<?php 

$con = mysqli_connect("localhost", "dbuser", "dbpass", "dbname"); 

$users = mysqli_query($con, "SELECT * FROM phpbb_user_group WHERE group_id='8'"); 
while($row = mysqli_fetch_array($users)) { 
     $developers[] = $row["user_id"]; 
} 

$post = mysqli_query($con, "SELECT * FROM phpbb_posts"); 
while($row = mysqli_fetch_array($post)) { 
     $topic_id = $row["topic_id"]; 
     $forum_id = $row["forum_id"]; 
     $post_id = $row["post_id"]; 
     $post_text = $row["post_text"]; 
     $post_time = $row["post_time"]; 
} 

$username = mysqli_query($con, "SELECT * FROM phpbb_users WHERE user_id='2'"); 
while($row = mysqli_fetch_array($username)) { 
     $postauthor = $row["username"]; 


if (strlen($post_text) > 10) 
    $post_text = wordwrap($post_text, 120); 
    $post_text = explode("\n", $post_text); 
    $post_text = $post_text[0] . '...'; 

$result = mysqli_query($con, "SELECT * FROM phpbb_posts WHERE poster_id='2' LIMIT 6"); 
while($row = mysqli_fetch_array($result)) { 
     $content = '<div onclick="location.href=\'http://test.mythros.net/forum/viewtopic.php?f=' . $forum_id .  '&amp;p=' . $topic_id . '#p' . $post_id . '\';" class="forum-latest-box">'; 
     $content .=  '<div class="forum-latest-userbar">'; 
     $content .=      '<div class="forum-latest-avatar">'; 
     $content .=        '<img src="https://minotar.net/helm/' . $postauthor . '/40.png">'; 
     $content .=      '</div>'; 
     $content .=      '<h1>' . $postauthor . '</h1>'; 
     $content .=    '</div>'; 
     $content .=    '<div class="forum-latest-content">'; 
     $content .=      '<div class="forum-latest-text">'; 
     $content .=        '"' . $post_text . '"'; 
     $content .=      '</div>'; 
     $content .=      '<div class="forum-latest-meta">'; 
     $content .=        gmdate("F j, Y, g:i a", $post_time); 
     $content .=      '</div>'; 
     $content .=    '</div>'; 
     $content .=  '</div>'; 
     echo $content; 
} 

?> 
+0

看看你的循环更详细地说,特别是你要通过帖子打印它们的循环。在那个循环中,你想获得关于作者的信息,做各种文本转换,然后打印出结果的文章。 '$ post_text'从哪里来?你什么时候设置的?它是否打印出您认为正在打印的内容? – 2014-09-10 23:27:41

+0

@ialarmedalien $ post_text 来自 $ post_text = $ row [“post_text”]; 从数据库中拉出phpbb文章的文本。它似乎正在展示我想要的东西,只是所有六个“条目”都显示了相同的帖子信息。 – Skulburn 2014-09-10 23:31:45

+0

查看$ post_text等正在设置的循环;它正在通过论坛上的所有帖子并将这些变量设置为适当的值。但是,它仅保存* last * post中的数据,因为每个循环会覆盖前一个循环中的数据。你现在明白了这个问题吗? – 2014-09-10 23:35:44

回答

1

您可以通过使用组合查询到phpbb_posts表和phpbb_users表在同一时间使用一个循环,让您的文章数据和用户信息解决这个问题:

## Line break added to the query for legibility 
$result = mysqli_query($con, 
"SELECT 
    p.post_id AS post_id, 
    p.topic_id AS topic_id, 
    p.forum_id AS forum_id, 
    p.post_time AS post_time, 
    p.post_subject AS subject, 
    p.post_text AS post_text 
    IFNULL(m.username, 'Guest') AS username, 
    FROM phpbb_posts AS p 
    LEFT JOIN phpbb_users AS m ON (m.user_id = p.poster_id) 
    ORDER BY phpbb_posts.post_time DESC LIMIT 6"); 

while($row = mysqli_fetch_array($result)) { 
# $row now contains the post information and the user info, so you can grab all your data, 
# process the post text, and print it out at the same time. 
    $post_text = $row["post_text"]; 
    # do your text transformation 
    if (strlen($post_text) > 10) 
    ... (etc.) 
    # now set up your content 
    $content = '<div onclick="location.href=\'http://test.mythros.net/forum/viewtopic.php?f=' . $row["forum_id"] .'&amp;p=' . $row["topic_id"] . '#p' . $row["post_id"] . '\';" class="forum-latest-box">'; 
    ... (etc.) 

} 
+0

非常好!以前从未真正查看过组合查询。很有帮助! +1 – Mallander 2014-09-11 17:10:36

相关问题