Friends system


First you need to run the following in PHPmyAdmin

PHP Code
  1. CREATE TABLE `friend_requests` (
  2. `id` int(10) NOT NULL auto_increment,
  3. `username` varchar(225) NOT NULL default '',
  4. `by` varchar(225) NOT NULL default '',
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=MyISAM;
  7.  
  8. CREATE TABLE `friends` (
  9. `id` int(10) NOT NULL auto_increment,
  10. `friendname` varchar(225) NOT NULL default '',
  11. `username` varchar(225) NOT NULL default '',
  12. PRIMARY KEY (`id`)
  13. ) ENGINE=MyISAM;


You may want to add them seperatly

Right i have tested this system so it should work

---

Call this page friendrequest.php
PHP Code
  1. <?
  2. session_start(); //starts session
  3. include "config.php"; //include config
  4.  
  5. if ($logged[username]){ //checks user is logged in
  6.  
  7. if ($_GET[user]){ //gets username
  8. $username = htmlspecialchars($_GET[user]); //friend
  9. $by = $logged[username]; //you
  10. $query = mysql_query("INSERT INTO `friend_requests` ( `username` , `by` ) VALUES ( '$username' , '$by' )"); //inserts the request
  11. echo ( "$username has been sent a request you must now wait for it to be accepted" ); //echos completion
  12. } else {
  13. echo ( "No request was made" ); // or no request sent
  14. }
  15. } else {
  16. echo ( "You need to be logged in" ); //not logged in
  17. }
  18. ?>


Ok, now call this one newfriends.php

PHP Code
  1. <?
  2. session_start(); // starts sessions
  3. include "config.php"; // inlcudes config
  4.  
  5. if ($logged[username]) { //checks user is logged in
  6. switch ($_GET[friends]) { //allows multiple pages
  7. default:
  8. $get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$logged[username]' "); //gets requests
  9. while ($reqs = mysql_fetch_array($get))
  10. {
  11.  
  12. echo ( "Friend Requests
  13. $reqs[by] wants to be friends with you.
  14. <a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a><br/>
  15. <a href='newfriends.php?friends=delete&user=$reqs[by]'>Delete</a>" ); //displays requests and shows accept delete links
  16. }
  17. break;
  18.  
  19. case 'accept': //accept page
  20. if ($_GET[user]) { //get username
  21.  
  22. $add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$logged[username]') "); // add to your friends list
  23. $delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
  24. echo ( "$_GET[user] has been added as a friend and the request has been deleted" ); // echos the completion
  25. }
  26. break; //ends accept page
  27.  
  28. case 'delete': // delete page
  29. if ($_GET[user]) { //gets username
  30. $delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
  31. echo ( "$_GET[user]'s request has been deleted" ); // echos completion
  32. }
  33. break; //ends delete page
  34. } // ends switch
  35. } else {
  36. echo ( "You need to be logged in" ); // not logged in
  37. }
  38. ?>


Right now to add the requests in members.php

Find:
PHP Code
  1. echo "<fieldset style='width: 350'>
  2. <b>$user[username]'s Profile</b><br><br>
  3. Email: $user[email]<br>
  4. Location: $user[location]<br>
  5. Sex: $user[sex]<br>
  6. Age: $user[age]


Add:
PHP Code
  1.  
  2. <a href='friendrequest.php?user=$user[username]'>Add as Friend</a>


Adn now go to the page where your links are..

should be login.php

add in there

PHP Code
  1. <a href='newfriends.php'>Friend Requests</a>


Any problems let me no ;)

Chris..
new2old's Avatar
Author:
Views:
4,951
Rating:
Posted on Thursday 21st August 2008 at 04:58 PM
saintpaulos
saintpaulos's Avatar
Actually I sorted it. Showed I learned something! lol Thanks! :)
Posted on Thursday 21st August 2008 at 02:09 PM
saintpaulos
saintpaulos's Avatar
I love this!

Its not a problem, but if you have no friends you get a blank page. How would you add, 'Sorry no friend requests'?
Posted on Friday 8th August 2008 at 05:17 AM
UrbanTwitch
UrbanTwitch's Avatar
Time to fix a bug. I found out that if you send someone a friend request and they accept. If you go to their user profile it shows you as their friend. But if you go to your profile (you'd expect to see them as your friend) it shows no friends.

How do we fix this?

Find in newrequests.php:

PHP Code
  1. $add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$logged[username]') "); // add to your friends list


and add below it:

PHP Code
  1. $add2 = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$logged[username]' , '$_GET[user') "); // fix friend bug


Enjoy!
Posted on Sunday 20th July 2008 at 07:25 PM
Dava
Dava's Avatar
ill write it later and post it for you
Posted on Sunday 20th July 2008 at 12:58 AM
UrbanTwitch
UrbanTwitch's Avatar
Can you do it Dava? :-P
Posted on Friday 18th July 2008 at 08:12 PM
Dava
Dava's Avatar
do an if class urban just say if $friendname = $username hide else show :P that should be pretty basic
Posted on Friday 16th May 2008 at 06:49 PM
jambomb
jambomb's Avatar
brill!
Posted on Tuesday 8th April 2008 at 05:57 PM
Dalez
Dalez's Avatar
Yeah, what Urban said, that would be cool.
Posted on Monday 7th April 2008 at 09:52 PM
UrbanTwitch
UrbanTwitch's Avatar
Is there a way to make it after you be someones friend the "Add As Friend" doesn't show up anymore?
Posted on Sunday 17th February 2008 at 11:42 AM
Crown
Crown's Avatar
Yes i am ? :3