Jump to content

Archived

This topic is now archived and is closed to further replies.

techiefreak05

friends system ???

Recommended Posts

I'm trying to figure out how i would have a "friends" or "buddies" feature.. like myspace.. how would i go about inserting what data where.. idk how to start with this.

Share this post


Link to post
Share on other sites
You could have a table which links id's

just 2 fields

id
friendid

so when they log in you will have the id then just query this table to get the id's of their friends

Ray

Share this post


Link to post
Share on other sites
well you could have a seperate table say called 'friends'with the fields 'username' and 'friend' and then simply add a new row for the user each time they add a friend and then to show you simply do "select friend from friends where username = '$username'" and echo all the users back otherwise you could use a unique row for each user and have friends comma seperated in the database.

justa  few idea's

Regards
Liam

Share this post


Link to post
Share on other sites
so i did something like this in phpMyAdmin. is this on the right track?

[code]CREATE TABLE `friends` (
`username` VARCHAR( 31 ) NOT NULL ,
`friend` VARCHAR( 31 ) NOT NULL
) TYPE = MYISAM ;
[/code]

Share this post


Link to post
Share on other sites
well you could do it that way. Only problem there is if the person changes his or her name. If you connect then thru their unique id, they can change every thing in the row and it will still link correctly.

You can do it the way you have it you like. If you do it by unique id you just need to change the field to an interger maybe 11 long

Ray

Share this post


Link to post
Share on other sites
They may not be able to change their username, but numbers are far easier and faster to query than strings. Plus you do not have to worry about stupid character which may be interpreted by php and sql incorrectly.

Well first thing is how you want them to find the friend. you want a dropdown menu of everyone in the table?? probably not but is an option. You can add it by them browsing to other peoples profiles. This would be the best way. when a person browses to someone elses profile, that persons id will be available. Also the person doing the browsing should have logged in and their id can be held in a session so you could have a link to a page which adds the id's

[code]<?
$id = $_SESSION['userid'];
$friend = $_POST['userid'];  //or $_GET whichever you prefer

$sql = "INSERT INTO friends SET
  userid = '".$id."',
  friend = '".$friend."'";[/code]

Hope that gets you on the right track, and hope I didn't mess you up with the session stuff

Ray

Share this post


Link to post
Share on other sites
i got it to work !! but .. now i have a prob. i want to add a send message button ... heres my code..

[code]<hr width=35%><br><br>
<b>Add Friend:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#66CCCC"><font color="black">Username:</font></td></tr>
<tr><td bgcolor="#66CCCC">"><input type="input" name="friendUN"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="friendNew" value="-Add Friend-"></td></tr>
</table>
</form>
<b>YourFriends:</b>
<?php
$query = mysql_query("SELECT * FROM friends WHERE `username` = '$_SESSION[username]'");
while ($array = mysql_fetch_array($query)){
echo "<br><center><b>" .$array['friend']. "</b></center>";
echo "<a href='sendMessage.php?friend='" .$array['friend']. "'><input type='submit' name='friend' value='-Send Message-'></a>";
}
?>
[/code]

this line:
[code]echo "<a href='sendMessage.php?friend='" .$array['friend']. "'><input type='submit' name='friend' value='-Send Message-'></a>";
[/code]

takes me to "sendMessage.php?friend="
why ...

Share this post


Link to post
Share on other sites
try


[code]<hr width=35%><br><br>
<b>Add Friend:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#66CCCC"><font color="black">Username:</font></td></tr>
<tr><td bgcolor="#66CCCC">"><input type="input" name="friendUN"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="friendNew" value="-Add Friend-"></td></tr>
</table>
</form>
<b>YourFriends:</b>
<?php
$query = mysql_query("SELECT * FROM friends WHERE `username` = '$_SESSION[username]'");
while ($array = mysql_fetch_array($query)){
$friend=$array['friend'];
echo "<br><center><b>" .$friend. "</b></center>";
echo "<a href='sendMessage.php?friend='" .$friend. "'><input type='submit' name='friend' value='-Send Message-'></a>";
}
?>[/code]

Share this post


Link to post
Share on other sites
in the sendMesage.php?friend= ....

I want the textbox called "to", to be filled with the value of "friend"

heres the "to" code

[code]<input type="text" name="to" value="<? echo $_GET[friend]; ?>"[/code]

and "to" is never filled with anything! help!!

Share this post


Link to post
Share on other sites
That's because your using POST in your form change

<form action="" method="post">

to

<form action="" method="get">

Share this post


Link to post
Share on other sites
well heres the code i have that echos all your friends and a send message button for each user:

[code]<?php
$query = mysql_query("SELECT * FROM friends WHERE `username` = '$_SESSION[username]'");
while ($array = mysql_fetch_array($query)){
$friend=$array['friend'];
echo "<br><center><b>" .$friend. "</b></center>";
echo "<form action='' method='get'><a href='sendMessage.php?friend='" .$friend. "'><input type='submit' name='friend' value='-Send Message-'></a></form>";

}
?>[/code]

the button STILL takes me to "sendMessage.php?friend="

Share this post


Link to post
Share on other sites
because you have nothing with the value of the get in your submitting the form change

echo "<form action='' method='get'><a href='sendMessage.php?friend='" .$friend. "'><input type='submit' name='friend' value='-Send Message-'></a></form>";


to

echo "<a href='sendMessage.php?friend='" .$friend. "'>Send Message</a>";

That will do the job :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.