RTS Posted September 17, 2006 Share Posted September 17, 2006 I'mn creatting a website like myspace with user profiles, but am having trouble thinking of how I would make a button to add a user to another users friends list. I am using MYSQL, and have all the username, password info and stuff in a table. Any ideas about how to do this? Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 17, 2006 Share Posted September 17, 2006 An easy way of doing this would be to have a db table called something like friends. with the following fields in it.[list][*]id[*]username or userid[*]friend_username or id[*]date_added[*]Now here is a simple example usage.[/list][code]<?phpsession_start();if (!$_SESSION['username']) { echo "You must be logged in to add a friend."; include("your_login_page.php"(; exit();}$username = $_SESSION['username'];$friends_username = mysql_real_escape_string(trim($_GET['u']));$sql = "INSERT INTO `friends` (`username`, `friends_username`, `date_added`) VALUES('$username, '$friends_username', now())";$res = mysql_query($sql) or die(mysql_error());if (!$res) { echo "Sorry unable to add you the user to your friends list at this time";}else{ echo "The user $friends_name was added to your friends list";}?>[/code]This is a rather simple example. You should get the basic idea.Hope this helps,Tom Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 17, 2006 Share Posted September 17, 2006 I almost forgot. When you display the users profile(the one getting added) have a like like this.thatscript.php?u=$theusername Quote Link to comment Share on other sites More sharing options...
RTS Posted September 17, 2006 Author Share Posted September 17, 2006 wont that make it so users can only one friend? Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 17, 2006 Share Posted September 17, 2006 No. If you use my example it will create a new id for each time a user adds a friend. To get the list of anyone users friends you can use a query like this.[code=php:0]$username = $_SESSION['username'];$sql = "SELECT * FROM `friends` WHERE `username` = '$username'";$res = mysql_query($sql);if (mysql_num_rows($res) == 0) { echo "You have no friends";}while ($rw = mysql_fetch_assoc($res)) { echo $rw['friends_name'] . '<br />";}[/code]So in short, each user could have as many friends in the table as they want.Good luck,Tom Quote Link to comment Share on other sites More sharing options...
RTS Posted September 19, 2006 Author Share Posted September 19, 2006 [quote author=tomfmason link=topic=108351.msg435850#msg435850 date=1158455711]I almost forgot. When you display the users profile(the one getting added) have a like like this.thatscript.php?u=$theusername[/quote]sorry, but how do I do this? Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 19, 2006 Share Posted September 19, 2006 I don't know how you are displaying the users profile but here is a simple form where the user can input another users username and add them to there friends list.[code]<?phpsession_start();if (!$_SESSION['username']) { echo "You must be logged in to view this page"; include("yourloginpage.php"); exit();}if (isset($_POST['submit'])) { if (empty($_POST['friend'])) { echo "You must enter a friends name"; exit(); } $username = $_SESSION['username']; $friend = mysql_real_escape_string(trim($_POST['friend'])); $q = mysql_query("SELECT COUNT(*) AS `friend_check` FROM `your_users_table` WHERE `username` = '$friend'") or die(mysql_error()); $friend_check = mysql_result($q, 0, 'friend_check'); if ($friend_check == 0) { echo "There is no user that matches that name. Please try again"; exit(); } $sql = mysql_query("INSERT INTO `friends` (`username`, `friends_username`, `date_added`) VALUES('$username', '$friend', now())") or die(mysql_error()); if ($sql) { echo "You friend <b>$friend</b> was added to your friends list.<br />"; }else{ echo "There was an error. Please contact the web master.<br />"; }}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Add a friend</title></head><body>Welcome <?php echo $_SESSION['username']; ?>,<br /> You can add a friend to your friends list with this form.<br /> Enjoy..!<br /><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Your Friends username<br /> <input type="text" name="friend" size="20" maxlength="20" /><br /> <input type="submit" name="submit" value="Add A Friend" /></form></body></html>[/code]Hope this helps,Tom Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 19, 2006 Share Posted September 19, 2006 tomfmason nice code but i think that entering the username is a wast so what i done was let the user press a link that says add this user to fav list from that users profile.but its all about prefrence ha lol. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 19, 2006 Share Posted September 19, 2006 [quote author=tomfmason link=topic=108351.msg435850#msg435850 date=1158455711]I almost forgot. When you display the users profile(the one getting added) have a like like this.thatscript.php?u=$theusername[/quote][quote author=tomfmason link=topic=108351.msg437019#msg437019 date=1158641450]I don't know how you are displaying the users profile [/quote]Yea I had no idea how he is displaying the users profile or what the content of the profile is so I just wrote this real quick.@RTS.. Redarrow is right in that you should have a link to a script simular to this(in each users profile) that will allow you to add the user to your friends list.I would be better able to explain what I mean if you would post the code that you use to display your user's profiles.@DarkHavn That was uncalled for. Why would you say something like that. Please do not reply to a post unless you have a suggestion, question or piece of code pretaining to the post in question...Good Luck,Tom Quote Link to comment Share on other sites More sharing options...
RTS Posted September 19, 2006 Author Share Posted September 19, 2006 example of a users profile:[code]<?phpsetcookie("user", "$user", time()+3600); ?><html><body><title>cocak's Profile</title><link rel="shortcut icon" href="logo.ico" type="image/x-icon" /><body bgcolor=<?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT bgcolor FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[bgcolor]);}?>><body background=<?php$sql2 = "SELECT bgimg FROM users WHERE username = '$user'";$result2 = mysql_query($sql2) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result2)) { print($row[bgimg]);}mysql_close($con);?> width=100% hight=100%><div align="center"><a href="home.html"> <embed src="images/yaddayay_banner.jpg"></embed></a><embed src="images/home.jpg" width="728" hight="30" usemap="#barmap"></embed><map id="barmap" name="barmap"><area shape="rect"coords="0,30,59,0" alt="Home"href="home.php"><area shape="rect"coords="60,30,104,0"alt="join"href="register.php"><area shape="rect"coords="105,30,262,0"alt="music"href="music.html"><area shape="rect"coords="263,30,336,0"alt="concerts"href="concerts.html"><area shape="rect"coords="337,30,452,0"alt="search"href="profile search.html"><area shape="rect"coords="453,30,513,0"alt="games"href="games.php"><area shape="rect"coords="514,30,619,0"alt="blogs"href="blogs.php"><area shape="rect"coords="615,30,730,0"alt="radio"href="radio.html"><table width=734 cellpadding=0 cellspacing=3><tr><td bgcolor=336699style="border-top:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-bottom:6px gray<?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-right:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-left:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;"><div align=center><FONT color=<?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT headcolor FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[headcolor]);}?> size=+5><MARQUEE bgcolor=#336699 direction=right loop=20 width=100%><STRONG><?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT headline FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[headline]);}?></STRONG></MARQUEE></FONT></DIV></td></tr></table><?if(file_exists("counters/cocak.dat")){ $exist_file = fopen("counters/cocak.dat", "r"); $new_count = fgets($exist_file, 255); $new_count++; fclose($exist_file); $exist_count = fopen("counters/cocak.dat", "w"); fputs($exist_count, $new_count); fclose($exist_count);}else{$new_file = fopen("counters/cocak.dat", "w");fputs($new_file, "1");fclose($new_file);}?><div style=" top: 200; left: 143; position: absolute; z-index: 1; visibility: show;"><table width=332 cellpadding=0 cellspacing=3><tr><td bgcolor=336699style="border-top:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-bottom:6px gray<?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-right:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-left:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;"><h3 align=center>cocak's Profile</h3><img src=images/pictures/<?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT pic FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[pic]);}mysql_close($con);?>></td></tr></table><div style=" top: 0; left: 406; position: absolute; z-index: 1; visibility: show;"><table width=332 cellpadding=0 cellspacing=3><tr><td bgcolor=336699style="border-top:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-bottom:6px gray<?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-right:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;border-left:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[tableborder]);}?>;"><h3 align=center>About Me</h3><?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT about FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { print($row[about]);}mysql_close($con);?></td></tr></table><form action=add.php method=post><input type=submit value=Add!/></form></body></html> [/code]add.php:[code]<?phpsession_start();$username = $_SESSION['username'];$con = mysql_connect("localhost","ZackBabtkis","");$friends_username = $_COOKIE["user"];if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("test", $con);$sql = "INSERT INTO `users` (`username`, `friends_username`) VALUES('$username', '$friends_username')";$res = mysql_query($sql) or die(mysql_error());if (!$res) { echo "Sorry unable to add you the user to your friends list at this time";}else{ echo "The user $friends_name was added to your friends list";}?>[/code]Thanks for all the help btw. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 19, 2006 Share Posted September 19, 2006 I wouldn't set the friends info into a cookie. All I would do is this...add this piece of code somewhere in your user profile.[code=php:0]$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { echo '<a href="add.php?u=' . $row['username'] . '">Add ' . $row['username'] . ' to your friends list</a>';}mysql_close($con);?>[/code]Now the add.php is pretty much like the first piece of code that I posted.[code]<?phpsession_start();if (!$_SESSION['username']) { echo "You must be logged in to add a friend."; include("your_login_page.php"(; exit();}$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$username = $_SESSION['username'];$friends_username = mysql_real_escape_string(trim($_GET['u']));$sql = "INSERT INTO `friends` (`username`, `friends_username`, `date_added`) VALUES('$username, '$friends_username', now())";$res = mysql_query($sql) or die(mysql_error());if (!$res) { echo "Sorry unable to add you the user to your friends list at this time";}else{ echo "The user $friends_name was added to your friends list";}mysql_close($con);?>[/code]Also, you do not have to connect and disconnect from the database so much. I understand why you thought you had to but when you have a long list of $row s being returned with very little html in between I would do something like this...[code]style="border-top:6px gray <?php$user = "cocak";$con = mysql_connect( "localhost", "ZackBabtkis", "") or die(mysql_error());mysql_select_db(test, $con);$sql = "SELECT tableborder FROM users WHERE username = '$user'";$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); // what's wrong?while($row = mysql_fetch_array($result)) { echo $row['tableborder'] .';border-bottom:6px gray ' . $row['tableborder'] . 'border-right:6px gray ' . $row['tableborder'] . ' border-left:6px gray ' .$row['tableborder'] . '"><h3 align=center>About Me</h3> ' . $row['about'];}mysql_close($con);?>[/code]This ^ is just meant to be an example. I would clean it up a bit. It whould give you the basic idea.Good Luck,Tom Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.