使用以下代码向用户发送请求(BELLOW IS JUST A FILE)
//addFriend functionality
if (isset($_SESSION['username']) && $_SESSION['username'] != "") {
$isFriend = false;
$reqSent = false;
$frd = getUsersFriends($user);
foreach ($frd as $f) {
if ($_SESSION['username'] == $f[0]) {
$isFriend = true;
}
}
$req = getUsersReqs($loggedIn);
#print_r($req);
foreach ($req as $r) {
if ($user == $r[0]) {
$reqSent = true;
}
}
if ($isFriend == false && $reqSent == false && $_SESSION['username'] != "$user") {
<form method="post" action="">
<input type="hidden" name="request" value="<?php echo $user; ?>">
<input type="submit" value="Add Friend"/>
</form>
<?php
} elseif ($isFriend == false && $reqSent == true && $_SESSION['username'] != "$user") {
echo "Friend Request Pending";
}
}
?>
但点击Add Friend
后,它应更改为Request Sent
但它不起作用
这里是上面代码中使用的函数
function getUsersFriends($user) {//gets the usernames of all friends of $user
global $db;
$usersFriends = array();
$stmt = $db->prepare('SELECT user2 FROM friends WHERE user1=:username');
$stmt->execute(array(':username' => $user));
$friends = $stmt->fetch(PDO::FETCH_ASSOC);
if (is_array($friends) || is_object($friends)) {
foreach ($friends as $newUser) {
array_push($usersFriends, $newUser);
}
}
#print_r($usersFriends);
return $usersFriends;
}
function getUsersReqs($user) {//gets the usernames of all request from $user
$usersReqs = array();
global $db;
$stmt = $db->prepare('SELECT user2 FROM requests WHERE user1=:username');
$stmt->execute(array(':username' => $user));
$requests = $stmt->fetch(PDO::FETCH_ASSOC);
foreach ($requests as $newUser) {
array_push($usersReqs, $newUser);
}
return $usersReqs;
}