php mysql在文本中创建Facebook类型用户友情通知

时间:2014-05-13 06:41:22

标签: php mysql

我已尽力以最好的方式结束所有事情,但一切都是徒劳的。我正在建立一个社区网站,我陷入了困境。当我的朋友与其他用户成为朋友时,我想在墙上显示文字。就像Facebook显示它一样。例如。

“John doe现在是max stone的朋友”

并且

“John doe现在是max stone和另外5个人的朋友”

我成功地展示了它,但不像我上面展示的那样。我使用来自通知表的while循环。我试过但不能像facebook上那样得到它。所以请帮我完成这件事。

我发布了我的代码,以便您可以清楚地了解我的代码以及我的代码中的错误,以便您可以清除。

$ check_if_friendship_created = mysql_query ("SELECT * FROM `users_notifications` 
WHERE `friend_1_username` IN (SELECT `friend_2_username` FROM `users_friends` 
WHERE `friend_1_username` = '". $ logged_user ['username']."') 
GROUP BY `friend_2_fullname` ORDER BY `notification_time` DESC");

也让大家都知道我朋友的桌子在设计上是对称的......

期待您的积极回应。谢谢....

1 个答案:

答案 0 :(得分:1)

为什么你没有单独的友情通知表?当user1是user2的朋友时,您将详细信息插入友谊通知表。

表格结构

id   reqeust_sent_by   request_accepted_by  friendship_date

因此,让我们假设John发送了朋友请求,然后Jack接受了请求,当Jack接受请求时,您将该详细信息插入到表中。你最好使用mysqli预处理语句或pdo。所以这里是mysqli准备好的陈述

 $request_sender  = 'John';
 $logged_user = 'Jack';
 $date =   date('Y-m-d H:i:s');
 $mydabtase  = new mysqli('localhost', 'root', '', 'database_name');
 $stmt = $mydatabse->prepare("insert into friendship_notification (request_sent_by, request_accepted_by, friendship_date) values (?,?,?)");
 $stmt->bind_param('sss' $request_sender, $logged_user, $date);
 $stmt->execute();
 $stmt->close();

现在选择你可以做的数据

 $stmt = $mydatabase->prepare("select * from friendship_notification where request_sent_by ! = ? or requst_accepted_by ! = ? order by id desc");//do your select here. here we are selecting where either rows are different from the logged in user because we don't want to show the logged in user that he has became friends with somebody else, we show this for other users.
 $stmt->bind_param('ss', $logged_user, $logged_user);//follow the same procedure when binding the parameters, s means string. if you have 2 ? then you need 2 s along with 2 variables.
 $stmt->execute();
 $result = $stmt->get_result();//this gets the results
 $total = $result->num_rows;//this returns the total number of rows for the above select query
 while($row = $result->fetch_assoc()){
 //if the total is less than 3 people we do the below
 if($total < 3){
 echo $row['request_accepted_by']."is now friends with".$row['request_sent_by'].",";
 }
 elseif($total > 3 ){
   $stmt2 = $mydatabase->prepare("select * from friendship_notification where request_sent_by ! = ? or requst_accepted_by ! = ? order by id desc limit 1");//do your select here
 $stmt2->bind_param('ss', $logged_user, $logged_user);
 $stmt2->execute();
 $result2 = $stmt2->get_result();
 $row2 = $result2->fetch_array();//we only need one row so no need for while loop.
   echo $row['request_accepted_by']."is now friends with".$row2['request_sent_by']."and ".$total-1." others.";
 }
 }

所以这会显示类似

的内容

John is now friends with Max and magna.和第二个案例

John is now friends with Max and 4 others.

如果你想要facebook方式,那么你需要使用ajax来自动刷新。