我正在做一个好友列表,但效果并不好。 好吧,我试图向所有用户的朋友展示但它不起作用,它只显示一个而不是全部。
在MySQL的friends_request表中我有这样的东西:
(我不知道如何在这里放置一个可绘制的表,所以我把表格的html )
<table border="1">
<tr>
<td>id</td>
<td>from_username</td>
<td>to_username</td>
<td>requested</td>
<td>accepted</td>
</tr>
<tr>
<td>1</td>
<td>username1</td>
<td>username2</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>username1</td>
<td>username3</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>username2</td>
<td>username3</td>
<td>0</td>
<td>1</td>
</tr>
</table>
它显示的只是一个用户名而不是用户名是用户登录的所有用户...例如,在'username1'中只显示为'username2'的朋友,而不是'username3'显示我希望知道原因。
<?php
$selfriends = "SELECT * FROM friends_request WHERE from_username = '$user' OR to_username='$user' AND accepted = '1'";
$resultfriend = $sql->query($selfriends); // select the table of friends_request from database
$rowfriend = mysqli_fetch_assoc($resultfriend); // calls the string, char or number from friends_request table
$t1 = $rowfriend['from_username']; // calls user from_username from friends_request table
$t2 = $rowfriend['to_username']; // calls user to_username from friends_request table
$accepted = $rowfriend['accepted']; // calls accepted from friends_request table
$fid = $rowfriend['id'];
/* CHECK IF FRIEND GOT SOME PICTURE */
$selPicturePro = "SELECT * FROM users WHERE username = '$t1'";
$resultsPicPro = $sql->query($selPicturePro); // select the users table and find out the content of pic
$rowProfiles = mysqli_fetch_assoc($resultsPicPro); // calls the row of profile picture.
if($rowProfiles > 1) {
$profileView = "<img src='".$rowProfiles['profile']."' width='auto' height='155px' />";
} else {
$profileView = "<img src='images/no-picture.png' width='auto' height='100px' />";
}
$selPicturePro2 = "SELECT * FROM users WHERE username = '$t2'";
$resultsPicPro2 = $sql->query($selPicturePro2); // select the users table and find out the content of pic
$rowProfiles2 = mysqli_fetch_assoc($resultsPicPro2); // calls the row of profile picture.
if($rowProfiles2 > 1) {
$profileView2 = "<img src='".$rowProfiles2['profile']."' width='auto' height='155px' />";
} else {
$profileView2 = "<img src='images/no-picture.png' width='auto' height='100px' />";
}
if($user != $t1) { // if the user is not the sender and accepted = 1, then the
// receiver sees the other user.
echo '<h3>Friends</h3>';
echo '<a href="profile.php?u='.$t1.'" class="no-style">';
echo $profileView;.'<br />'.$t1;
echo '</a>';
}
else if($user != $t2) { // if the user is not the receiver and accepted = 1, then the
// sender see the other user.
echo '<h3>Friends</h3>';
echo '<a href="profile.php?u='.$t2.'" class="no-style">';
echo $profileView2.'<br />'.$t2;
echo '</a>';
}
if($user != $t1 && $accepted == 0) {
echo "<br clear='all' />";
echo "<h3>Friends</h3>";
echo "You don't have friends yet.";
}
if($user != $t2 && $accepted == 0) {
echo "<br clear='all' />";
echo "<h3>Friends</h3>";
echo "You don't have friends yet.";
}
?>
答案 0 :(得分:0)
你需要一个循环来处理所有的朋友。此外,您可以将所有查询与联接组合在一起。
您可以更改查询,将其同时放入包含from_username
的单个列,而不是复制to_username
和UNION
的所有代码。
在处理行时无法完成用户是否有朋友的测试,因为当您没有任何朋友时,将不会选择任何行。在循环之前执行此操作,方法是检查返回的行数。
<?php
$selfriends = "SELECT fr.username, u.profile
FROM (SELECT from_username AS username
FROM friends_request
WHERE to_username = '$user' AND accepted = 1
UNION ALL
SELECT to_username AS username
FROM friends_request
WHERE from_username = '$user' AND accepted = 1) AS fr
JOIN users AS u ON u.username = fr.username";
$resultfriend = $sql->query($selfriends); // select the table of friends_request from database
if ($resultfriend->num_rows == 0) {
echo "<br clear='all' />";
echo "<h3>Friends</h3>";
echo "You don't have friends yet.";
} else {
echo '<h3>Friends</h3>';
while ($rowfriend = mysqli_fetch_assoc($resultfriend)) { // calls the string, char or number from friends_request table
$t1 = $rowfriend['username'];
$profile = $rowfriend['profile'];
if(!empty($profile)) {
$profileView = "<img src='".$profile."' width='auto' height='155px' />";
} else {
$profileView = "<img src='images/no-picture.png' width='auto' height='100px' />";
}
<a href="profile.php?u=<?php echo $t1; ?>" class="no-style">
<?php echo $profileView; ?><br />
<?php echo $t1; ?>
</a>
<?php
}
}