PHP朋友2行之间的关系?

问题描述:

first time I've used this site .. I've had a good look around but I cant seem to find the answer to my question. It's been answered in other ways but not for what I want I think..

Image of the database table so it's easy to see: http://puu.sh/6BtmZ.png

So basically I've got a friends.php page that has an input field and submit button, you put a username in and press submit and I want it to send a request to that user; I've got that to work, the user can accept but it only places the "friend" into him. As in FRIENDA is friends with FRIENDB but on FRIENDB's page he isn't friends with FRIENDA .. I hope this makes sense.

I just basically want it so, you send a request and the person accepts it and BOTH parties can see each other as friends.

 <?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'; ?>

Hopefully someone might be able to help? Thanks in advance!

What you probably want to do is insert both directions in the friends relationship once both friends have confirmed. So instead of doing this:

INSERT INTO `users_friends` (user_id, friends_with) VALUES ('$user_id', '$username_id')

You would do this:

INSERT INTO `users_friends` (user_id, friends_with) VALUES ('$user_id', '$username_id'),('$username_id','$user_id')

This would allow you to do the friend lookup in either direction.

What is unclear to me however is how you store pending friend requests in your database. So what you may ultimately need to do is to insert those relations in the DB and then simply update those DB records to change the value of an "accepted" flag once the friendship has confirmed.

If the structure of "users_friends" table looks like this:

me        friends_with      request_pending

to get the result (= if the user is your friend):

"SELECT * FROM `users_friends` WHERE (`friends_with` = '$user_id' AND `me`= `$me`) OR (`friends_with` = '$me' AND `me`= `$user_id `) AND `request_pending` = 0"

You check if the user is in friends_with where YOU are in me.. or if the user is in me and you're in friends_with