Jump to content


Photo

Add to friends


  • Please log in to reply
10 replies to this topic

#1 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 17 September 2006 - 12:52 AM

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?

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 17 September 2006 - 01:13 AM

An easy way of doing this would be to have a db table called something like friends. with the following fields in it.

  • id
  • username or userid
  • friend_username or id
  • date_added


  • Now here is a simple example usage.
<?php
session_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";
}
?>

This is a rather simple example. You should get the basic idea.

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 17 September 2006 - 01:15 AM

I almost forgot. When you display the users profile(the one getting added) have a like like this.
thatscript.php?u=$theusername


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 17 September 2006 - 01:49 AM

wont that make it so users can only one friend?

#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 17 September 2006 - 03:09 AM

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.

$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 />";
}

So in short, each user could have as many friends in the table as they want.

Good luck,
Tom
 

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 19 September 2006 - 03:37 AM

I almost forgot. When you display the users profile(the one getting added) have a like like this.
thatscript.php?u=$theusername



sorry, but how do I do this?

#7 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 19 September 2006 - 04:50 AM

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.

<?php
session_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>

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 19 September 2006 - 05:02 AM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 19 September 2006 - 05:11 AM

I almost forgot. When you display the users profile(the one getting added) have a like like this.
thatscript.php?u=$theusername



I don't know how you are displaying the users profile


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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#10 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 19 September 2006 - 06:09 AM

example of a users profile:
<?php
setcookie("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=336699
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)) {
  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=336699
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)) {
  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=336699
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)) {
  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> 

add.php:
<?php
session_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";
}
?>
Thanks for all the help btw.

#11 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 19 September 2006 - 12:30 PM

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.
$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);
?>

Now the add.php is pretty much like the first piece of code that I posted.

<?php
session_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);
?>

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...

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);
?>

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users