簡體   English   中英

PHP的MySQL在文本中創建Facebook類型的用戶友誼通知

[英]php mysql create Facebook type users friendship notification in text

據我所知,我竭盡全力結束一切,但徒勞無功。 我正在建立一個社區網站,但我一心一意。 當我的朋友與其他用戶成為朋友時,我想在牆上顯示文本。 就像Facebook顯示它一樣。 例如。

“約翰·多伊現在是麥克斯通的朋友”

“ 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");

還讓大家知道我的朋友桌在設計上是對稱的...

期待您的積極回應。 謝謝....

您為什么沒有單獨的友誼通知表? 含義是當user1與user2成為朋友時,請將詳細信息插入友誼通知表。

表結構

id   reqeust_sent_by   request_accepted_by  friendship_date

因此,假設約翰發送了好友請求,然后傑克接受了請求,當傑克接受請求時,您將該詳細信息插入到表中。 您最好使用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自動刷新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM