Building your own Myspace.com with PHP part V: Friends and guestbook

What is a community without friends? Not much really… So thats why this tutorial will cover how to add friends to the users presentation as bookmarks. We will also go over how to search the database for new friends to add to your list.

The code we write in this tutorial all goes in the same file (members.php) that we started in the previous tutorial. First lets look at how to add a new friend.

if(isset($_GET["add"]))
{ // add to your list of friends
	require_once("classes/DbConnector.php");
	$username = $_SESSION["username"];
	$friend = $_GET["add"];
	$db = new DbConnector();
	$db->connect();
	$query = "SELECT * FROM friends WHERE member='$username' AND friendwith='$friend'";
	$result = $db->query($query);
	$exist = mysql_num_rows($result);  // Does the row exists?
	if($exist=='0')
	{
		$query = "INSERT INTO friends(member,friendwith) VALUES('$username','$friend')";
		$db->query($query);
		echo "You are now friends with $friend";
	}
	else
	{
		echo "$friend is already in your list of friends!";
	}
}

We call this code with members.php?add=friend where “friend” is the friend we want to add to our list. This function should speak for itself by now but what we do is that we first check if the friend we are trying to add already exists in the list if not we add it to the database.

In order to get new friends there has to be some kind of search function. This is extremely easy to accomplish with our setup.

if(isset($_GET["find"]))
{	// Search for a user
	require_once("classes/DbConnector.php");
	$username = $_POST["username"];
	$db = new DbConnector();
	$db->connect();
	$query = "SELECT * FROM members WHERE username LIKE '%$username%'";
	$result = $db->query($query);
	$exist = mysql_num_rows($result);  // Does the row exists?
	if($exist=='0')
	{
		echo "No match found";
	}
	else
	{
		echo "Your matches for string: $username

";
		while($currow = mysql_fetch_array($result))
		{
			echo "".$currow["username"]."
"; } } }

>

We use this code by calling members.php?find=username where username is our search string. The cool thing here is this row:
“SELECT * FROM members WHERE username LIKE ‘%$username%’”;
The LIKE function compares our search string with the usernames stored in the database and if the username contains the letters in the string they are returned.
So we don’t need to search for the full username, if we type in the letter a all entries in the database containing an a will be returned.

Ok we are now almost done, just one feature left. The guestbook.

if(isset($_GET["sign"]))
{	// Sign the guestbook
	require_once("classes/DbConnector.php");
	$username = $_GET["sign"];
	$from = $_GET["from"];
	$message = $_POST["message"];
	$time = date("F j, Y, g:i a");
	$db = new DbConnector();
	$db->connect();
	$query = "INSERT INTO guestbook(owner,postedby,post,time) VALUES('$username','$from','$message','$time')";
	$db->query($query);
	echo "Your message has been posted";

}

We will also need to add the following to our view function that we made in part 4 to make the guestbook visible on the presentation.

// Display guestbook
$owner = $rows["username"];
$query = "SELECT * FROM guestbook WHERE owner='$member' ORDER BY ID DESC";
$result = $db->query($query);
while($gbrows = mysql_fetch_array($result))
	{
		echo "Posted by: ".$gbrows["postedby"]." - ".$gbrows["time"]."
".$gbrows["post"]." "; } echo "

Sign $owner:s guestbook

";

Next part: Finishing touches

Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post
 |  TrackBack URI for this post

2 Responses to “Building your own Myspace.com with PHP part V: Friends and guestbook”

  1. Great ! I’m just missing some important functions, like deleting or editing comments as profile owner.

  2. Hello guys
    Wasjust serfing on net and found this site…want to say thanks. Great site and content!

Leave a Reply