【问题标题】:Display own and friend posts, with php使用 php 显示自己和朋友的帖子
【发布时间】:2019-07-24 05:50:03
【问题描述】:

我想显示我自己的帖子(默认),但如果Status = '1'也显示朋友的帖子

我只有这段代码,它显示所有带有相关图像和内容的帖子,因为我不知道如何创建我正在寻找的代码。

数据库结构

帖子:

| commentid |  comment  | imagesid | author |
---------------------------------------------
|     1     |   fool    |   5557   | test   |
|     2     |  fool2    |   5585   | devel  |
---------------------------------------------

多个图像:

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

连接:

| id |  Friend | FriendReferee | Status | 
-----------------------------------------
| 1  |  test   |     devel     |    1   |
-----------------------------------------

当前的抓取结构

获取图片的实际代码:

 $sql = "SELECT * FROM multiple_image";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $imgs[$row['imagesid]][$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->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
      $commentsToImages[$row['commentid']] = $row['imagesid'];
      $comments[$row['commentid']] = $row['comment']; 
  }     
 }

在分隔的 div 中显示结果

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


<?php echo $comments[$commentID];?>
  </div>
  <div class='pics'>
    <?php
          foreach($imgs[$imagesID] as $img) { 
                  echo $img;
          }
    ?>
  </div>
</div>

<?php
}
?>

我尝试使用 JOIN 和 if else 语句来实现,但并没有真正按照我的意愿工作。

【问题讨论】:

  • iamgesid 上的 posts 表是否是问题或数据库中的拼写错误?
  • @Quasimodo'sclone,是的,它已修复。 :)
  • 对我来说,您的表结构实际上是什么还不够清楚。我希望表 posts(id, user_id, comment, image_id)connections(user_id,friend_id, ... ),都引用 user(id, user_name, ...)。然后你可以在用户 ID 上建立一个 JOIN。
  • 您应该根据 id 而不是冗余名称来规范化数据库。应该有一个包含名称的主表。这是基于查询中的 id 连接的。 en.wikipedia.org/wiki/Database_normalization
  • 是的,我只显示显示带有图像的 cmets 的代码,因为我不知道如何使用此代码显示自己和朋友的帖子。我想,有人可以使用我目前拥有的代码并制作我正在寻找的代码。

标签: php arrays loops mysqli foreach


【解决方案1】:

userspostsconnections 表上应该有一个 user_idfriend_id 还引用了users 表的user_id

然后你JOINuser_id 上的表postsusers。通过 id 查找用户名来检索作者。

SELECT
 `p`.`post_id`,
 `p`.`content`    `Post Content`,
 `u`.`user_name`  `Author`
FROM
  `posts`        `p`
INNER JOIN 
  `users`        `u`  USING(`user_id`)
WHERE 
  `p`.`user_id` = 1
;

现在您可以LEFT JOIN 表格connections 发布您想查看的朋友的帖子。您想过滤所有具有特定user_idposts 或加入的connection 表具有特定friend_id,例如1:

SELECT
 `p`.`post_id`,
 `p`.`content`    `Post Content`,
 `u`.`user_name`  `Author`
FROM
  `posts`        `p`
INNER JOIN 
  `users`        `u`  USING(`user_id`)
LEFT JOIN
  `connections`  `c`  ON(`c`.`show_posts`  AND  `p`.`user_id` = `c`.`friend_id`)
WHERE 
  1 in (`p`.`user_id`, `c`.`user_id`)
ORDER BY `p`.`post_id`
;

本例使用的数据库结构为:

CREATE TABLE `users`
(
  user_id    INT  NOT NULL  AUTO_INCREMENT  PRIMARY KEY,
  user_name  VARCHAR(20)
)
;

CREATE TABLE `connections`
(
  `user_id`    INT  NOT NULL,
  `friend_id`  INT  NOT NULL,
  `show_posts` BOOL,
  PRIMARY KEY(`user_id`, `friend_id`)
)
;

CREATE TABLE `posts`
(
  `post_id`   INT  NOT NULL  AUTO_INCREMENT  PRIMARY KEY,
  `user_id`   INT  NOT NULL,
  `content`   TEXT(2000)
)
;

INSERT INTO `users`
  (`user_name`)
VALUES
  ('Tom'  ),
  ('Sarah'),
  ('Ben'  )
;

INSERT INTO `connections`
VALUES
  (1,2, true ), -- Tom/Sarah, Tom wants Sarah's posts
  (2,1, false), -- Sarah/Tom, Sarah does not want Tom's posts
  (1,3, false), -- Tom/Ben,   Tom does not want Ben's posts
  (3,1, true )  -- Ben/Tom,   Ben does not want Tom's posts
;

INSERT INTO `posts`
  (`user_id`, `content`)
VALUES
  (1, 'First post by Tom.'   ),
  (2, 'First post by Sarah.' ),
  (1, 'Second post by Tom.'  ),
  (3, 'First post by Ben.'   ),
  (3, 'Second post by Ben.'  ),
  (2, 'Second post by Sarah.'),
  (3, 'Third post by Ben.'   )
;

Try online

【讨论】:

  • 如何使用此数据库方案显示朋友(如果是)和我自己的帖子以及相关图像和内容?
  • 所以我必须更新我的数据库方案和查询?
  • 您可以更改您的表格以获得与演示类似的方案。
  • 好的。还有一件事。我是否仍然需要使用我目前使用的数组循环方法来在您的方法中将文本和图像分隔在不同的 div 中?
  • 您可以为所有帖子设置一个主循环,包括自己的和朋友的。如果帖子只有一张图片,您也可以加入。否则,您可以执行内部循环。
猜你喜欢
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 2012-01-24
  • 1970-01-01
相关资源
最近更新 更多