如何显示在分离DIV评论所有相关的图像和重复的主要DIV在PHP中的新项目吗?(How to dis

2019-11-05 11:50发布

我要为在一个盒子里的注释显示所有相关的图像组合在一起但在不同的div喜欢的东西,我已经附着在图像上显示评论。

如果有一个新的职位,然后重复使用PHP的主要股利。

我想不通,我应该怎么做。 我认为我必须从某个地方打破循环。

数据库结构

帖子:

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

multiple_image:

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

我想是这样的

 .main { background-color: #e3e3e3; width: 400px; padding: 5px 4px; margin-bottom: 10px; border-radius: 2px; } .comments { padding: 10px; background-color: #eee; } .pics { padding: 10px; background-color: #c3c3c3; } 
 <div class='main'> <div class='comments'> Related Comment here </div> <div class='pics'> Related Images here </div> </div> <!-- repeat the same thing with a new post --> <div class='main'> <div class='comments'> Related Comment here </div> <div class='pics'> Related Images here </div> </div> <!-- etc --> <div class='main'> <div class='comments'> Related Comment here </div> <div class='pics'> Related Images here </div> </div> 


用于获取图像的代码和相关评论

$sql = "SELECT * FROM images
    JOIN posts ON (images.imagesid=posts.imgs)
    ORDER BY  postID";
  $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 ($row['name'] == NULL) {
        $imgs= '';
      } else {
        $imgs= "<img width='' src='../images/".$row['name']."' >";
      }

  if($comment != $row['content']){
      echo $row['content'];
     $comment = $row['content'];
  }
    echo $imgs;
  } 

 }

Answer 1:

不得不追查你的转贴,对不起了这么久才回到你身边。 工作干扰。 您的代码不符合您的数据库列,所以我不知道天气数据库的例子是正确的,或者你的代码。 我猜的数据库,但如果我猜错了,你就必须更换相应的数据库字段。

我简化了数据库查询,因为我认为你知道自己在做什么,是不是比有效率很重要。

$sql = "SELECT * FROM multiple_image";
  $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()) {
        $imgs[$row['imageid]][$row['id']]= "<img width='' src='../images/".$row['name']."' >"; // array of image id's, with arrays of images inside them.
  } 

 }
$sql = "SELECT * FROM posts";
  $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()) {
      $commentsToImages[$row['commentid']] = $row['imagesid']; // array of post id's to picture ids
      $comments[$row['commentid']] = $row['comment']; // array of post id's to comment text.
  } 

 }

现在你只需要渲染你的漂亮的新阵列。

foreach($commentsToImages as $commentID =>$imagesID) {
    ?>
<div class='main'>
  <div class='comments'>


<?php echo $comments[$commentID]; // render the comment ?>
  </div>
  <div class='pics'>
    <?php
          foreach($imgs[$imagesID] as $img) { // foreach loop that will render all the images...
                  echo $img;
          }
    ?>
  </div>
</div>

<?php
}
?>

有可能是一个错字或两个在那里,但这些代码应该做你想要什么。 我要澄清,这不是要做到这一点的最好办法。 它不遵循最佳做法,也不是最有效的。 但是,如果你刚开始学习的每个循环,它需要最少的新东西要学。

  1. 简单的数据库查询。
  2. 阵列
  3. 每个循环。
  4. 如果再发言。

你应该回去,并添加如果再声明赶上各种潜在的错误。 我不知道,如果你的数据库允许例如空值,但他们可以扔扳手。 但是,它的工作无论哪种方式。



文章来源: How to display all related images with a comment in separated div and repeat the main div for a new entry in php?
标签: php html mysqli