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
}
?>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!