Building your own Myspace.com with PHP part V: Friends and guestbook
Posted by Stefan on July 31st, 2007 at 09:00pm
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<br><br>"; while($currow = mysql_fetch_array($result)) { echo "<a href=\"member.php?id=".$currow["username"]."\">".$currow["username"]."</a><br/>"; } } }
>
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: <a href=\"member.php?id=".$gbrows["postedby"]."\">".$gbrows["postedby"]."</a> - ".$gbrows["time"]."<br/>".$gbrows["post"]."<br><br>"; } echo "<center><br/><b>Sign $owner:s guestbook</b><br/> <form action=\"member.php?sign=$owner&from=".$_SESSION["username"]."\" method='POST'> <textarea name='message' rows='5' cols='30' align='left'>Your message</textarea><br/> <input type='submit' value='Update' name='submit'> </form> </center> ";
Next part: Finishing touches
2 Comments for Building your own Myspace.com with PHP part V: Friends and guestbook
1. Nils | August 2nd, 2007 at 4:06 pm
Great ! I’m just missing some important functions, like deleting or editing comments as profile owner.
2. Brargo | April 1st, 2008 at 2:13 am
Hello guys
Wasjust serfing on net and found this site…want to say thanks. Great site and content!
Leave a Comment for Building your own Myspace.com with PHP part V: Friends and guestbook
Trackback this post | Subscribe to the comments via RSS Feed