我第一次使用這個網站..我有一個很好的看看,但我似乎無法找到我的問題的答案。它在其他方面,但不是我想要的,我認爲已經回答了..2行之間的PHP朋友關係?
數據庫表的圖像,因此很容易看到:http://puu.sh/6BtmZ.png
所以基本上我有一個具有一個friends.php頁輸入欄和提交按鈕,你把一個用戶名,並按提交,我希望它發送一個請求,該用戶;我有這個工作,用戶可以接受,但它只是把「朋友」放入他。正如FRIENDA是FRIENDB的朋友,但在FRIENDB的頁面上,他不是FRIENDA的朋友..我希望這是有道理的。
我只是基本上想要它,你發送一個請求,這個人接受它,兩個派對可以看到對方作爲朋友。
<?php
include 'core/init.php';
// check
protect_page();
include 'includes/templates/header.php';
$user_id = $user_data['user_id'];
if (empty($_POST) === false) {
$required_fields = array('username');
foreach($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'You need to enter a username to send a request!';
break 1;
}
}
if (empty($errors) === true) {
if (user_exists($_POST['username']) === false) {
$errors[] = 'Sorry, the username \'' . htmlentities($_POST['username']) . '\' doesn\'t exist.';
}
}
}
if (isset($_GET['success']) && empty($_GET['success'])) {
echo 'Friend request sent!';
} else {
if (empty($_POST) === false && empty($errors) === true) {
// add friend
$username = $_POST['username'];
$get_userid = mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'");
$row = mysql_fetch_array($get_userid);
$username_id = $row[user_id];
$user_id = $user_data['user_id'];
mysql_query("INSERT INTO `users_friends` (user_id, friends_with) VALUES ('$user_id', '$username_id')");
//redirect
header('location: friends.php?success');
//exit
exit();
} else if (empty($errors) === false) {
// output errors
echo output_errors($errors);
}
}
?>
<h1>Friends</h1>
<p>
<h2>Send a friend request:</h2>
<form id="friend_request" action="" method="POST">
<ul>
<li>Username: <input type="text" name="username"><input id="friend_request" type="submit"></li>
</ul>
</form>
</p>
<p>
<h2>Pending requests:</h2>
<?php
if(isset($_POST['decline'])){
$decline_id = $_POST['decline_friend_id'];
$decline_query = "DELETE FROM `users_friends` WHERE `id` = $decline_id";
$accept_result = mysql_query($decline_query);
}
if(isset($_POST['accept'])){
$accept_id = $_POST['accept_friend_id'];
$accept_query = "UPDATE `users_friends` SET `request_pending` = 0 WHERE `id` = $accept_id";
$accept_result = mysql_query($accept_query);
}
$result = mysql_query("SELECT * FROM `users_friends` WHERE `friends_with` = '$user_id' AND `request_pending` = 1");
while($requests_row = mysql_fetch_array($result)) {
$get_username = mysql_query("SELECT `username` FROM `users` WHERE `user_id` = '$requests_row[user_id]'");
$get_username_row = mysql_fetch_array($get_username);
$request_from = $get_username_row[username];
echo 'Request from: ' . $request_from . '<form id="decline" action="" method="POST">
<input type="hidden" name="decline_friend_id" value="' . $requests_row['id'] . '">
<input type="submit" value="Decline" name="decline">
</form>
<form id="accept" action="" method="POST">
<input type="hidden" name="accept_friend_id" value="' . $requests_row['id'] . '">
<input type="submit" value="Accept" name="accept">
</form>';
echo '<br />';
}
?>
</p>
<h2>Your friends:</h2>
<?php
$friends_result = mysql_query("SELECT * FROM `users_friends` WHERE `friends_with` = '$user_id' AND `request_pending` = 0");
while($friends_row = mysql_fetch_array($friends_result)) {
$get_username = mysql_query("SELECT `username` FROM `users` WHERE `user_id` = '$friends_row[user_id]'");
$get_username_row = mysql_fetch_array($get_username);
$friend = $get_username_row[username];
echo $friend . '<br />';
}
?>
</p>
<?php include 'includes/templates/footer.php'; ?>
希望有人能夠幫忙嗎?提前致謝!
也感謝你,這聽起來更多就像我想要的,雖然我從來沒有做過這樣的查詢。雖然我只是進入PHP,所以我會給它前! – user3245541
好吧,只是試了一下,它一切正常。它增加了2行,基本上反過來是很好的。雖然它是作爲對我和其他人的待決要求而出現的。 我得到的是如果你「接受」的請求(這只是一個列表,如果「pending = 1」),這將變爲0意味着他們是你的朋友。我怎樣才能讓它發送請求,但不發送給我自己?也可能刪除,好像我刪除了朋友,我希望它刪除這兩個條目。 – user3245541
@ user3245541刪除是相同的概念:'DELETE FROM users_friends WHERE(user_id = $ user_id AND friends_with = $ username_id)或(friends_with = $ user_id AND user_id = $ username_id)'我不確定你的邏輯是關於發送請求的,但顯然你只是過濾掉表中用戶標識與用戶用戶標識相同的情況。 –