【问题标题】:How to display the comment only once but display all images from mysql in PHP?如何只显示一次评论,但在 PHP 中显示来自 mysql 的所有图像?
【发布时间】:2019-07-16 13:22:32
【问题描述】:

我只想将评论显示一次,例如 (testsetest) 与相关图像(通过连接两个表具有相同的 imageid)。

示例(我想要实现的): 评论:傻瓜图片:name1,name 2

Caption of the wrong output.

数据库结构

帖子:

| commentid |  comment  | iamgesid |
------------------------------------
|     1     |   fool    |   5557   |
|     2     |  fool2    |   5585   |
------------------------------------

多个图像:

| id |  image  | imagesid |
---------------------------
| 1  |  name1  |    5557  |
| 2  |  name2  |    5557  |
| 3  |  name3  |    5585  |
---------------------------

这是我当前的代码:

$sql = "SELECT image, posts.imagesid, multiple_image.imagesid, comment
        FROM multiple_image JOIN posts ON (multiple_image.imagesid=posts.imagesid)";
$result = $conn->query($sql);

if (!$result) {
    trigger_error('Invalid query: ' . $conn->error);
}

if ($result->num_rows > 0) {

    // output data of each row
    while($row = $result->fetch_assoc()) {

echo $row['comment'];
$imgs= "<div id='img_div'><img width='' src='upload/".$row['image']."' ></div>";
echo $imgs;
}
}

【问题讨论】:

    标签: php mysql database mysqli


    【解决方案1】:

    希望对你有所帮助:

    $sql    = "SELECT 
              image, 
              posts.imagesid, 
              multiple_image.imagesid, 
              comment
            FROM
               multiple_image 
            JOIN posts ON (multiple_image.imagesid=posts.imagesid)
            ORDER BY 
               commentid";
    $result = $conn->query($sql);
    if (!$result) {
        trigger_error('Invalid query: ' . $conn->error);
    }
    if ($result->num_rows > 0) {
        $comment = array();
        while ($row = $result->fetch_assoc()) {
            // check imagesid is not exist in array
            if (in_array($row['imagesid'], $comment) == false) {
                echo $row['comment'];
                $comment[] = $row['imagesid'];
            }
            $imgs = "<div id='img_div'><img width='' src='upload/" . $row['image'] . "' ></div>";
            echo $imgs;
        }
    }
    

    【讨论】:

    • 我们也可以这样使用:if (!in_array($row['imagesid'], $comment)) { echo $row['comment']; $comment[] = $row['imagesid']; }
    【解决方案2】:

    您需要通过commentid 控制中断结果和顺序

    请查看更新后的代码。

    
    $sql = "SELECT 
              image, 
              posts.imagesid, 
              multiple_image.imagesid, 
              comment
            FROM
               multiple_image 
            JOIN posts ON (multiple_image.imagesid=posts.imagesid)
            ORDER BY 
               commentid
    ";
    
    $result = $conn->query($sql);
    
    if (!$result) {
        trigger_error('Invalid query: ' . $conn->error);
    }
    
    
    if ($result->num_rows > 0) {
    
        // output data of each row
        $comment = '';
        while($row = $result->fetch_assoc()) {
          if($comment != $row['comment']){
             echo $row['comment'];
             $comment = $row['comment'];
          }
    
            $imgs= "<div id='img_div'><img width='' src='upload/".$row['image']."' ></div>";
            echo $imgs;
        }
    }
    

    【讨论】:

    • 那仍然没有达到我想要的效果。
    • 你能再解释一下吗?
    • 我尽量说得更清楚。我想连接两个表之间的 imageid,并且只显示一次评论,相关图像可能不止一个。我希望它更清楚一点。
    • 好的,我的例子不适合,因为?每组相同 imageid 的图像只打印一条评论。?
    • 哦,对不起,它看起来很有效
    【解决方案3】:

    您可以创建一个标志并检查它是否为 0。如果为 0,则您可以显示评论,否则您不需要显示评论。检查以下代码:

    if ($result->num_rows > 0) {
    
        // output data of each row
        $loop = 0;
        while ($row = $result->fetch_assoc()) {
            if ($loop == 0) {            
                echo $row['comment'];
            }
            $imgs = "<div id='img_div'><img width='' src='upload/" . $row['image'] . "' ></div>";
            echo $imgs;
            $loop++;
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 这将打印一次评论,而不是每组一次。
    • 我已将if ($loop == 0) 更改为if ($loop == 1),它只显示一次评论。你能解释一下为什么吗?
    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 2016-01-22
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 2017-01-13
    • 1970-01-01
    相关资源
    最近更新 更多