Friends request in PHP

Reply

Join Date: Mar 2008
Posts: 217
Reputation: mrcniceguy is an unknown quantity at this point 
Solved Threads: 4
mrcniceguy mrcniceguy is offline Offline
Posting Whiz in Training

Friends request in PHP

 
0
  #1
Nov 7th, 2008
I`m creating a social network website,My problem is that i dont know How to get started in this FRIENDS SECTOR.
I want somebody to be able to send a Friend Request to Another in my website,and after the other one Accepted the request Both to be able to see each in their friends list.
Anyone who have idea on how i can solve this i will appreciate.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,073
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: Friends request in PHP

 
1
  #2
Nov 8th, 2008
in my view,
create a table called firends,when ever one member send a request to the another member,the insert all the records in friends table like,memberid,accept,friendid...at first make the accept is 0..that means,the member is sent a request to his friend,but his friend doesn't accept the request...if his friend accept ,then update the friends table accept is 1,then show in their friends list....
Follow this for every request...
Any doubts,post here ......
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 217
Reputation: mrcniceguy is an unknown quantity at this point 
Solved Threads: 4
mrcniceguy mrcniceguy is offline Offline
Posting Whiz in Training

Re: Friends request in PHP

 
0
  #3
Nov 8th, 2008
thankx for your idea))also i found this working)))
  1. -- phpMyAdmin SQL Dump
  2. -- version 2.11.9.2
  3. -- http://www.phpmyadmin.net
  4. --
  5. -- Host: localhost
  6. -- Generation Time: Nov 08, 2008 at 05:19 AM
  7. -- Server version: 5.0.67
  8. -- PHP Version: 5.2.6
  9.  
  10. SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
  11.  
  12. --
  13. -- Database: `www`
  14. --
  15.  
  16. -- --------------------------------------------------------
  17.  
  18. --
  19. -- Table structure for table `friend_requests`
  20. --
  21.  
  22. CREATE TABLE IF NOT EXISTS `friend_requests` (
  23. `id` int(11) NOT NULL auto_increment,
  24. `sender` int(11) NOT NULL,
  25. `recipient` int(11) NOT NULL,
  26. PRIMARY KEY (`id`)
  27. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  28.  
  29. -- --------------------------------------------------------
  30.  
  31. --
  32. -- Table structure for table `members`
  33. --
  34.  
  35. CREATE TABLE IF NOT EXISTS `members` (
  36. `id` int(11) NOT NULL auto_increment,
  37. `username` varchar(255) NOT NULL,
  38. `friends` text NOT NULL,
  39. PRIMARY KEY (`id`)
  40. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
  41.  
  42. --
  43. -- Dumping data for table `members`
  44. --
  45.  
  46. INSERT INTO `members` (`id`, `username`, `friends`) VALUES
  47. (1, 'test', ''),
  48. (2, 'bob', ''),
  49. (3, 'chuck', '');
php code
  1. <?php
  2. session_start();
  3. mysql_connect("localhost", "root", "");
  4. mysql_select_db("www");
  5.  
  6. //START OF LOGIN
  7. if(!isset($_SESSION["logged"])) {
  8. if(isset($_POST["username"])) {
  9. $query = mysql_query("SELECT id FROM members WHERE username = '" . $_POST["username"] . "'");
  10. if(mysql_num_rows($query) > 0) {
  11. $row = mysql_fetch_array($query);
  12. $_SESSION["logged"] = $row["id"];
  13. header("Location: " . $_SERVER["PHP_SELF"]);
  14. }
  15. } else {
  16. echo("<form method=\"POST\">
  17. <input type=\"text\" name=\"username\" value=\"Type username here\">
  18. <input type=\"submit\" name=\"submit\">
  19. </form>");
  20. }
  21. } else {
  22. //END OF LOGIN
  23.  
  24. //START OF ADD FRIEND
  25. if(isset($_GET["add"])) {
  26. $query = mysql_query("SELECT id FROM members WHERE id = '" . $_GET["add"] . "'");
  27. if(mysql_num_rows($query) > 0) {
  28. $_query = mysql_query("SELECT * FROM friend_requests WHERE sender = '" . $_SESSION["logged"] . "' AND recipient = '" . $_GET["add"] . "'");
  29. if(mysql_num_rows($_query) == 0) {
  30. mysql_query("INSERT INTO friend_requests SET sender = '" . $_SESSION["logged"] . "', recipient = '" . $_GET["add"] . "'");
  31. }
  32. }
  33. }
  34. //END OF ADD FRIEND
  35.  
  36. //START OF ACCEPT FRIEND
  37. if(isset($_GET["accept"])) {
  38. $query = mysql_query("SELECT * FROM friend_requests WHERE sender = '" . $_GET["accept"] . "' AND recipient = '" . $_SESSION["logged"] . "'");
  39. if(mysql_num_rows($query) > 0) {
  40.  
  41. $_query = mysql_query("SELECT * FROM members WHERE id = '" . $_GET["accept"] . "'");
  42. $_row = mysql_fetch_array($_query);
  43.  
  44. $friends = unserialize($_row["friends"]);
  45. $friends[] = $_SESSION["logged"];
  46.  
  47. mysql_query("UPDATE members SET friends = '" . serialize($friends) . "' WHERE id = '" . $_GET["accept"] . "'");
  48.  
  49. $_query = mysql_query("SELECT * FROM members WHERE id = '" . $_SESSION["logged"] . "'");
  50. $_row = mysql_fetch_array($_query);
  51.  
  52. $friends = unserialize($_row["friends"]);
  53. $friends[] = $_GET["accept"];
  54.  
  55. mysql_query("UPDATE members SET friends = '" . serialize($friends) . "' WHERE id = '" . $_SESSION["logged"] . "'");
  56. }
  57. mysql_query("DELETE FROM friend_requests WHERE sender = '" . $_GET["accept"] . "' AND recipient = '" . $_SESSION["logged"] . "'");
  58. }
  59. //END OF ACCEPT FRIEND
  60.  
  61. //START OF SHOW FRIEND REQUESTS
  62. $query = mysql_query("SELECT * FROM friend_requests WHERE recipient = '" . $_SESSION["logged"] . "'");
  63. if(mysql_num_rows($query) > 0) {
  64. while($row = mysql_fetch_array($query)) {
  65. $_query = mysql_query("SELECT * FROM members WHERE id = '" . $row["sender"] . "'");
  66. while($_row = mysql_fetch_array($_query)) {
  67. echo $_row["username"] . " wants to be your friend. <a href=\"" . $_SERVER["PHP_SELF"] . "?accept=" . $_row["id"] . "\">Accept?</a><br />";
  68. }
  69. }
  70. }
  71. //END OF SHOW FRIEND REQUESTS
  72.  
  73. //START OF MEMBERLIST
  74. echo "<h2>Member List:</h2>";
  75. $query = mysql_query("SELECT * FROM members WHERE id != '" . $_SESSION["logged"] . "'");
  76. while($row = mysql_fetch_array($query)) {
  77. $alreadyFriend = false;
  78. $friends = unserialize($row["friends"]);
  79. if(isset($friends[0])) {
  80. foreach($friends as $friend) {
  81. if($friend == $_SESSION["logged"]) $alreadyFriend = true;
  82. }
  83. }
  84. echo $row["username"];
  85. $_query = mysql_query("SELECT * FROM friend_requests WHERE sender = '" . $_SESSION["logged"] . "' AND recipient = '" . $row["id"] . "'");
  86. if(mysql_num_rows($_query) > 0) {
  87. echo " - Friendship requested.";
  88. } elseif($alreadyFriend == false) {
  89. echo " - <a href=\"" . $_SERVER["PHP_SELF"] . "?add=" . $row["id"] . "\">Add as friend</a>";
  90. } else {
  91. echo " - Already friends.";
  92. }
  93. echo "<br />";
  94. }
  95. //END OF MEMBERLIST
  96.  
  97. //START OF FRIENDLIST
  98. echo "<h2>Friend List:</h2>";
  99. $query = mysql_query("SELECT friends FROM members WHERE id = '" . $_SESSION["logged"] . "'");
  100. while($row = mysql_fetch_array($query)) {
  101. $friends = unserialize($row["friends"]);
  102.  
  103. if(isset($friends[0])) {
  104. foreach($friends as $friend) {
  105. $_query = mysql_query("SELECT username FROM members WHERE id = '" . $friend . "'");
  106. $_row = mysql_fetch_array($_query);
  107. echo $_row["username"] . "<br />";
  108. }
  109. }
  110. }
  111. //END OF FRIENDLIST
  112. }
  113. ?>
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Friends request in PHP

 
0
  #4
Nov 10th, 2008
You could use the friend_requests table as the list of friends if you added an accepted field do it.

eg: viewing friends

$userid = intval( $_SESSION['logged']);

select f.* from friend_requests
left join members as m on (m.id = f.sender OR m.id = f.recipient)
WHERE m.id = $userid AND f.accepted = 1

Depending on the purpose that may be better, or worse then having a serialized list of friend ids. If you needed complex queries, like "friends in common" or "most friendly" then keeping the friends list in an indexed table like friend_requests would be better.

You'd want to put an index on each column of friend_request if you use it that way though.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC