我开发了一个简单的上传照片和评论系统。当我为每个使用时,它重复图像。例如,如果对照片有5条评论。它用5个不同的评论重复了5次照片。而不是一个图像5评论。
我希望每条评论都有1张图片。如何在不重复照片的情况下获取每张照片的所有评论?
我使用PDO,这是php代码:
<?php
//This is for the images
$results = $connecDB->prepare("select image,caption,id from gallery order by id desc LIMIT $start, $limit ");
$results->execute();
$results = $results->fetchAll();
foreach($results as $results) {
$pid=$results["id"];
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
foreach($re as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p>
<p><?php echo $results['caption']?></p>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
<div class="large-10 columns"><p><?php echo $comment?></p></div>
<?php}
}?>
感谢。
答案 0 :(得分:0)
你在内部循环中循环所有内容,请尝试这样做。
category
答案 1 :(得分:0)
实际上你没有在 $ re 变量中得到结果。执行查询后,您需要 fetchAll 数据。除了你在内在的foreach中循环所有东西。
foreach($results as $results) {
$pid=$results["id"];
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
$comments = $re->fetchAll();
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p>
<p><?php echo $results['caption']?></p>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
foreach($comments as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="large-10 columns"><p><?php echo $comment; ?></p></div>
<?php}
}?>