Making a Friend List

Post all your tuts or request for tuts here.
Post Reply
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Making a Friend List

Post by 62896dude »

Hello comrades!

So after a long night last night, I figured out how to make a friend list, and I was so happy, that I decided I would make a nice quaint little tutorial here for it! So let's get started!

First, in your database, create a new table called "friends", with the following fields (I don't really care how many values you allow in each one):

user_id - int
friend_id - int
user_name - varchar
friend_name - varchar

Then, you are going to want to create the form where you enter the user that you want to add as a friend using the following code, which I called add_friend.php:

Code: Select all

<html>
<head>
</head>
<body>
<?php
//this will just start your session by connecting to your database and checking to make sure that you are logged in, 
//the usual stuff
include_once 'connect.php';
session_start();

include_once 'logo.php';
?>
 <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login2" div align="center">


<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}
//this is where the "usual stuff" ends
?>
</div>

<?php
//fetching the player's information from the db
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
$bypass = 0
 ?>
//we are starting to make the form, friendck will be explained soon
<form name="message" action="friendck.php"
method="post">
//next is where you actually type in the user's name
Add Friend: <input type="text" name="add"><br>
</body>
</html>
<?php
echo "<br><br><br><a href='index.php'>Go Back</a>";
?>

Now that you have created the quite basic form for adding friends, it is time to create the friendck.php that was referenced in the form:

Code: Select all

<?php
include_once 'connect.php';
session_start();

include_once 'logo.php';
?>
 <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login2" div align="center">


<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}
?>
</div>

<?php
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
$bypass = 0
 ?>
<?php

$newfriend=$_POST['add'];
$uid=$playerinfo3['id'];
$fid=$newfriend['id'];
$uname=$playerinfo3['name'];

$ck_friendlist = "SELECT friend_name FROM friends WHERE friend_name = '".$newfriend."'";
$ck_friend = "SELECT name FROM players WHERE name = '".$newfriend."'";

        //this will just make sure that the user you are adding is a valid user, because why add someone that doesn't 
        //exist?
        if( mysql_num_rows( mysql_query( $ck_friend ) ) == 0 ){
die("The user you are trying to add doesn't exist. Please go back and try again.<br>
<form name=\"back\" action=\"add_friend.php\"
method=\"post\">
<input type=\"submit\" value=\"Try Again\">
</form>
");
}
//this ensures that you aren't already friends with that user, it checks the db to see if it is repeating itself pretty
// much
elseif( mysql_num_rows( mysql_query( $ck_friendlist ) ) !== 0 ){
echo "You already are friends with this user, go back and add another person!<br><br>";
echo "<a href='add_friend.php'>Go Back</a>";

}
//this checks to make sure that the name you entered isn't your own
elseif( $newfriend = $player ){
echo "You can't add yourself! Go back and add someone else!<br><br>";
echo "<a href='add_friend.php'>Go Back</a>";

}
else{
//this is the stage where it has passed all of the checks and is okay, now it is adding the values to the "friends" 
//table 
mysql_query("INSERT INTO friends (user_id, friend_id, user_name, friend_name) VALUES ('$uid','$fid','$uname','$newfriend')") OR die("Could not add the player: <br>".mysql_error()); 
echo "Friend was successfully added!";
?>
//this is a basic button that allows the user to go back and add another friend
<form name="back" action="add_friend.php"
method="post">
<input type="submit" value="Add another friend">
</form>
<?php
}
?>
And there you have it! By the way, please note two things:

1) I explained the code in the comments, so be sure to read them!

2) You may have to adjust some variables and other aspects to suit your own database

Thanks for reading this, and if you have any questions/find any errors, just let me know! See ya!
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Making a Friend List

Post by Nexus »

Wow great job! The code is nice, neat, and understandable even without the comments and I enjoy reading tutorials that are like that! :)
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: Making a Friend List

Post by 62896dude »

Yeah, that's the great thing about PHP, if you know the language well enough, it literally just reads through like a book! :)
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Making a Friend List

Post by Torniquet »

nice work :)

My first friends system worked pretty much the same way, except i didnt hold their usernames. (if i ever want to get data like that i query the user table.)

My site now uses a string of id's to form a friends list. These are exploded and imploded as necessary.

Nice tut none the less :)
New Site Coming Soon! Stay tuned :D
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: Making a Friend List

Post by 62896dude »

Thank you! And I will look into the way you do it at some point, that sounds like I may rather use that!
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Making a Friend List

Post by Torniquet »

it does require extra db calls to get the info you need, but i find it much neater.

Let me know if you need help with it.
New Site Coming Soon! Stay tuned :D
Kevin1230
Posts: 62
Joined: Mon Jul 18, 2011 3:32 pm

Re: Making a Friend List

Post by Kevin1230 »

Looks nice, i had a problem, but managed to fix it
Kevin1230
Posts: 62
Joined: Mon Jul 18, 2011 3:32 pm

Re: Making a Friend List

Post by Kevin1230 »

It says "You can add yourself, go back and add someone else!" I'm sure I put someone elses username on
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: Making a Friend List

Post by 62896dude »

I'm sure there are problems with it, hahaha, I haven't look at this script in a while though. From what I saw, your problem MIGHT be fixed by adding another = to if($newfriend = $player)

Another possible thing for you to do is instead of the elseif statements, try just doing if statements for all of them (except for at the bottom where it says else, keep that)

Again, I have no clue if these will fix your problem, but it is the best I can think of, lol

And out of curiosity, what error did you find/fix earlier?
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
dust1031
Posts: 92
Joined: Fri Jul 22, 2011 3:38 am

Re: Making a Friend List

Post by dust1031 »

i got the add friend button working, but how can the players look at the friends list, and show how many friends they have and if there friends are online.
Post Reply

Return to “Tutorials”