我正在尝试创建一个社交脚本,但是我从用户表中检索帖子表和用户头像的帖子和我的朋友时遇到了一些麻烦 我可以得到自己的所有帖子,或者只收集我的朋友而不是我的帖子。我如何获得我和我的朋友的所有帖子以及添加帖子的用户的个人资料图片
$all_friends = array();
$sql = "SELECT user1 FROM friends WHERE user2='$u' AND accepted='1' ORDER BY RAND() ";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user1"]);
}
$sql = "SELECT user2 FROM friends WHERE user1='$u' AND accepted='1' ORDER BY RAND()";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user2"]);
}
$orLogic = '';
foreach($all_friends as $key => $user){
$orLogic .= "username='$user' OR ";
}
$orLogic = chop($orLogic, "OR ");
$sql = "SELECT username, avatar FROM users WHERE $orLogic";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$fusername = $row["username"];
$favatar = $row["avatar"];
if($favatar != ""){
$fpic = 'user/'.$fusername.'/'.$favatar.'';
} else {
$fpic = 'img/male.png';
}
这是我的朋友代码使用$ fusername我可以得到他们所有的帖子,但我怎么能把我的帖子发送到
答案 0 :(得分:0)
您的代码看起来应该可以正常工作。但也许可以尝试将3个SQL语句合并为1个
SELECT friends.user1 as user, users.avatar
FROM friends
INNER JOIN users ON users.username = friends.user1
WHERE friends.user2 = '$u' AND friends.accepted = '1'
UNION
SELECT friends.user2 as user, users.avatar
FROM friends
INNER JOIN users ON users.username = friends.user1
WHERE friends.user1 = '$u' AND friends.accepted = '1'
ORDER BY RAND()