dounou9751 2014-01-28 17:18
浏览 52
已采纳

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!

  • 写回答

2条回答 默认 最新

  • duagfgfn1981 2014-01-28 17:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧