Hello to Everyone,
I m involving in social networking website in php.Can anybody help me for building add friend system in php?

Recommended Answers

All 2 Replies

So, you have a database. Every user must have an ID number. Then, you have one page, "people" page, and on this page are all people. And there are links next to every person name, like <a href="target.php">Add this person as friend</a>. As href you can use persons id, like this: href="addfriend.php?id=(personid)". And then, when somebody click on this link, the person ID will be saved in MYSQL table. So, people will have, in mysql table, something like this: data..., "friends: 1,2,3,4,62,549,16465,852212123", and when you want to read "friend", you have to use explode:

function getdata($id){
         //mysql connect....
         return $data; //name, nickname...
}
$friends="(their ids)"
$friends=explode(",",$friends);
echo "List of your friends:<br>";
foreach($friends as $friend){
         echo "".getdata($friend)."<br>";
}

PS. maybe better not to use ID, use code for every user, like this: 14fgd4ggh78h, you can make this code with this script:

$possible_characters="qwertzuiopasdfghjklxcyvbn0123456789"; //etc.
$code="";
for($i=1;$i<=20;$i++){
        $number=mt_rand(0,(strlen($possible_characters)-1));
        $code.=$possible_characters[$number];
}

But one better way is to create a file with friend's ID code. (eg. everyone has folder "friends" and in this folder there are files like 1dhdS45fAf5d6, sh4Ezhsg4hT, etc.)

@ivan3510
I think it's better to store friend's id on separate rows, it's easier to maintain, count and so on:

user_id | friend_id
-------------------
    1   |    23
    1   |    90
    2   |    37

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.