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.

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 ......

Comments
Good Analysing .....

thankx for your idea))also i found this working)))

-- phpMyAdmin SQL Dump
-- version 2.11.9.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 08, 2008 at 05:19 AM
-- Server version: 5.0.67
-- PHP Version: 5.2.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `www`
--

-- --------------------------------------------------------

--
-- Table structure for table `friend_requests`
--

CREATE TABLE IF NOT EXISTS `friend_requests` (
  `id` int(11) NOT NULL auto_increment,
  `sender` int(11) NOT NULL,
  `recipient` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `members`
--

CREATE TABLE IF NOT EXISTS `members` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `friends` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `members`
--

INSERT INTO `members` (`id`, `username`, `friends`) VALUES
(1, 'test', ''),
(2, 'bob', ''),
(3, 'chuck', '');

php code

<?php
session_start();
mysql_connect("localhost", "root", "");
mysql_select_db("www");

//START OF LOGIN
if(!isset($_SESSION["logged"])) {
  if(isset($_POST["username"])) {
    $query = mysql_query("SELECT id FROM members WHERE username = '" . $_POST["username"] . "'");
    if(mysql_num_rows($query) > 0) {
      $row = mysql_fetch_array($query);
      $_SESSION["logged"] = $row["id"];
      header("Location: " . $_SERVER["PHP_SELF"]);
    }
  } else {
    echo("<form method=\"POST\">
    <input type=\"text\" name=\"username\" value=\"Type username here\">
    <input type=\"submit\" name=\"submit\">  
    </form>");
  }
} else {
//END OF LOGIN

//START OF ADD FRIEND
  if(isset($_GET["add"])) {
    $query = mysql_query("SELECT id FROM members WHERE id = '" . $_GET["add"] . "'");
    if(mysql_num_rows($query) > 0) {
      $_query = mysql_query("SELECT * FROM friend_requests WHERE sender = '" . $_SESSION["logged"] . "' AND recipient = '" . $_GET["add"] . "'");
      if(mysql_num_rows($_query) == 0) {
        mysql_query("INSERT INTO friend_requests SET sender = '" . $_SESSION["logged"] . "', recipient = '" . $_GET["add"] . "'");
      }
    } 
  }
//END OF ADD FRIEND

//START OF ACCEPT FRIEND
  if(isset($_GET["accept"])) {
    $query = mysql_query("SELECT * FROM friend_requests WHERE sender = '" . $_GET["accept"] . "' AND recipient = '" . $_SESSION["logged"] . "'");
    if(mysql_num_rows($query) > 0) {
      
      $_query = mysql_query("SELECT * FROM members WHERE id = '" . $_GET["accept"] . "'");
      $_row = mysql_fetch_array($_query);
      
      $friends = unserialize($_row["friends"]);
      $friends[] = $_SESSION["logged"];      
                
      mysql_query("UPDATE members SET friends = '" . serialize($friends) . "' WHERE id = '" . $_GET["accept"] . "'");
      
      $_query = mysql_query("SELECT * FROM members WHERE id = '" . $_SESSION["logged"] . "'");
      $_row = mysql_fetch_array($_query);
      
      $friends = unserialize($_row["friends"]);
      $friends[] = $_GET["accept"];      
                
      mysql_query("UPDATE members SET friends = '" . serialize($friends) . "' WHERE id = '" . $_SESSION["logged"] . "'");
    }
    mysql_query("DELETE FROM friend_requests WHERE sender = '" . $_GET["accept"] . "' AND recipient = '" . $_SESSION["logged"] . "'");
  }
//END OF ACCEPT FRIEND

//START OF SHOW FRIEND REQUESTS
  $query = mysql_query("SELECT * FROM friend_requests WHERE recipient = '" . $_SESSION["logged"] . "'");
  if(mysql_num_rows($query) > 0) {
    while($row = mysql_fetch_array($query)) { 
      $_query = mysql_query("SELECT * FROM members WHERE id = '" . $row["sender"] . "'");
      while($_row = mysql_fetch_array($_query)) {
        echo $_row["username"] . " wants to be your friend. <a href=\"" . $_SERVER["PHP_SELF"] . "?accept=" . $_row["id"] . "\">Accept?</a><br />";
      }
    }
  }
//END OF SHOW FRIEND REQUESTS

//START OF MEMBERLIST
  echo "<h2>Member List:</h2>";
  $query = mysql_query("SELECT * FROM members WHERE id != '" . $_SESSION["logged"] . "'");
  while($row = mysql_fetch_array($query)) {
    $alreadyFriend = false;
    $friends = unserialize($row["friends"]);
    if(isset($friends[0])) {
      foreach($friends as $friend) {
        if($friend == $_SESSION["logged"]) $alreadyFriend = true;
      }
    }
    echo $row["username"];
    $_query = mysql_query("SELECT * FROM friend_requests WHERE sender = '" . $_SESSION["logged"] . "' AND recipient = '" . $row["id"] . "'");
    if(mysql_num_rows($_query) > 0) {
       echo " - Friendship requested.";
    } elseif($alreadyFriend == false) {
       echo " - <a href=\"" . $_SERVER["PHP_SELF"] . "?add=" . $row["id"] . "\">Add as friend</a>";
    } else {
      echo " - Already friends.";
    }
    echo "<br />";
  }
//END OF MEMBERLIST

//START OF FRIENDLIST
  echo "<h2>Friend List:</h2>";
  $query = mysql_query("SELECT friends FROM members WHERE id = '" . $_SESSION["logged"] . "'");
  while($row = mysql_fetch_array($query)) {
    $friends = unserialize($row["friends"]);
    
    if(isset($friends[0])) {
      foreach($friends as $friend) {
        $_query = mysql_query("SELECT username FROM members WHERE id = '" . $friend . "'");
        $_row = mysql_fetch_array($_query);
        echo $_row["username"] . "<br />";
      }
    }
  }
//END OF FRIENDLIST
}
?>

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

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.

This article has been dead for over six months. Start a new discussion instead.