gpt4 book ai didi

php - 如何在 PHP 中只显示一次评论但显示来自 mysql 的所有图像?

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:45 27 4
gpt4 key购买 nike

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

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

Caption of the wrong output.

数据库结构

posts:

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

multiple_image:

| 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;
}
}

最佳答案

您将需要通过 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;
}
}

关于php - 如何在 PHP 中只显示一次评论但显示来自 mysql 的所有图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54827244/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com