繁体   English   中英

使用 php 显示自己和朋友的帖子

[英]Display own and friend posts, with php

我想显示我自己的帖子(默认),但如果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 语句来做,但并没有像我想要的那样工作。

您应该在userspostsconnections表上有一个user_id 另外friend_id指的是users表的user_id

然后在user_idJOINpostsusers 通过 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 JOINconnections ,其中帖子来自您想要查看其帖子的朋友。 要过滤所有posts其中有一个特定user_id或加入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.'   )
;

在线试用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM