duanlei0282 2014-09-10 23:01
浏览 86
已采纳

显示六个最新的PhpBB帖子,而不是六个最新帖子

Trying to display six of the latest posts from my PhpBB installation. I'm happy with how it's all working, however it's showing six copies of the same (most recent) post, and not size unique latest posts.

Just to confirm, I have seven total posts on the forums.

<?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("
", $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;
}

?>
  • 写回答

1条回答 默认 最新

  • douhao2548 2014-09-11 00:01
    关注

    You can solve this problem by using a single loop and getting your post data and user information at the same time using combined query to the phpbb_posts table and phpbb_users table:

    ## 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.)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)