...

View Full Version : adding user as a friend



runnerjp
05-09-2008, 03:04 PM
hey guys im abit stuck on this one and was wondering... how would i make it so users can add other users as friends like on profile sites??

TheShaner
05-09-2008, 03:47 PM
friend_table
-----------
username (text)
friendname (text)
friend_placement (integer)
user_placement (integer)

The table above stores the username of the person who requested the friend add, stores their friend's username that was added, and the friend's placement, which is the location in their "top" list. Now, to prevent duplicate entries and redundant data, we also use this same table to track that the person who was requested can add the requester in their "top" list, and so this position would be stored in user_placement.


function getFriends($username)
{
$strSQL = "SELECT friendname AS friend, friend_placement AS placement " .
"FROM friend_table " .
"WHERE username = '$username' " .
"UNION "
"SELECT username AS friend, user_placement AS placement " .
"FROM friend_table " .
"WHERE friendname = '$username' " .
"ORDER BY placement ASC, friend ASC";
// execute above query, retrieve friend names, and return friend list
}
The query above retrieves the friends they've requested plus the friends that have requested them, orders the whole list by the placements, and then the remainder are sorted alphabetically.

I'm sure there are other methods that people may use, but this is what I came up with.

-Shane

runnerjp
05-09-2008, 09:24 PM
that still confuses me lol

any more help lol

_Aerospace_Eng_
05-09-2008, 09:31 PM
What exactly confuses you about it? Its pretty straight forward. Perhaps you should read some books or do some tutorials.

runnerjp
05-09-2008, 09:37 PM
well how would i go about getting the other users id?
and once i have this how would i post them into db

my_id | friends_ids
1 2,3
2 2

aedrin
05-09-2008, 11:53 PM
You should know this stuff by now. You've been here for a long time and you still don't seem to grasp the basic concepts of a website.

Maybe too many people have fed you working code...

runnerjp
05-10-2008, 02:24 PM
humm ok so to get a users username...seems i use RewriteRule ^([^/.]+)/?$ members/index.php?page=profile&username=$1

i could get it byyyy

<a href='friendrequest.php?user=$username'>Add as Friend</a>

on the lines i think but its the $username im not so sure

then the firend request page would beeeeee


<?php
session_start(); //starts session
include "../settings.php"; //include config

{

if ($_GET[user]){ //gets username
$username = htmlspecialchars($_GET[user]); //friend
$by = $_session[username]; //the person
$query = mysql_query("INSERT INTO `friend_requests` ( `username` , `by` ) VALUES ( '$username' , '$by' )"); //inserts the request
echo ( "$username has been sent a request you must now wait for it to be accepted" ); //echos completion
} else {
echo ( "No request was made" ); // or no request sent
}
}
?>

this will work just grabbing the username of the friend

runnerjp
05-10-2008, 03:51 PM
ahh ok im getting Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/runningp/public_html/members/friendrequest.php on line 8


PHP Code:

<?php
session_start(); //starts session
include "../settings.php"; //include config
{
if ($_GET['user']){ //gets username
$username = htmlspecialchars($_GET['user']); //friend
$by = $_session['username']; //the person
$query = mysql_query("INSERT INTO `friend_requests` ( `username` , `by` ) VALUES ( '$username' , '$by' )"); //inserts the request
echo ("$username has been sent a request you must now wait for it to be accepted"); //echos completion
} else {
echo ("No request was made"); // or no request sent
}
}
?>


now i cant see any whitespace or t_encapsed can u :S

idalatob
05-10-2008, 04:08 PM
I realised how stupid my reply was.... so I deleted it.

Sorry.

runnerjp
05-10-2008, 04:37 PM
ok chnaged it to this


<?php
session_start(); //starts session
include "../settings.php"; //include config
{
if ($_GET['user']){ //gets username
$username = htmlspecialchars($_GET['user']); //friend
$by = $_session['username']; //the person
$query = mysql_query "INSERT INTO `friend_requests` (`username`,`by`) VALUES ('$username','$by')"; //inserts the request
echo ("$username has been sent a request you must now wait for it to be accepted"); //echos completion
} else {
echo ("No request was made"); // or no request sent
}
}
?>

but line 8 =$query = mysql_query "INSERT INTO `friend_requests` (`username`,`by`) VALUES ('$username','$by')"; //inserts the request still not right and i cant see where :S

runnerjp
05-10-2008, 07:41 PM
ok guys been attacking it and iv finally come to a stop sign again

im now getting
Warning: Missing argument 1 for get_username(), called in /home/runningp/public_html/members/friendrequest.php on line 13 and defined in /home/runningp/public_html/functions.php on line 309

soooo


<?php
session_start();

include "../settings.php";

if ( !empty($_GET['user']) ) {
if ( ctype_alnum($_GET['user']) && strlen($_GET['user']) <= 10 ) {
// further test and escape data
}
$username =($_GET['user']);
$id = $_SESSION['user_id'];

$get_username_value = get_username();
$query = "INSERT INTO friends (friendname , username) VALUES('$_GET[user]', '$get_username_value')";
$result= mysql_query($query)
or die("QUERY ERROR:<br />{$query}<br />" . mysql_error() );

echo "$username has been sent a request you must now wait for it to be accepted";
// useful in troubleshooting the SQL statement


} else {
echo "No request was made";
}
?>

im getting the users id via $_SESSION['user_id']; this obviusly works as i have done it even in this script just6 to get users id

so my function is


/**
* Get username - Returns the username of the logged in member based on session ID
*
* @access public
* @param string
* @return string/bool
*/


function get_username ( $id )
{
global $db;

$query = "SELECT `Username` FROM `" . DBPREFIX . "users` WHERE `ID` = " . $db->qstr ( $id );

if ( $db->RecordCount ( $query ) == 1 )
{
$row = $db->getRow ( $query );

return $row->Username;
}
else {
return FALSE;
}
}

rafiki
05-10-2008, 08:28 PM
You need to add the $id variable when calling the function

_Aerospace_Eng_
05-10-2008, 08:29 PM
Sighs here we go again, we've already told you about this problem. get_username REQUIRES that you pass it the id of the user. Rather than using username=$username just use userid=$id and then when calling get_username do something like

get_username(intval($_GET['id']))
of course I don't know what your function is doing with $id so that may not be secure enough.

runnerjp
05-10-2008, 08:41 PM
ahhh yes thanks...took it onboard and messed around and got it too work..

iv started on my other bit of code but i have an error with this one

<?
session_start(); // starts sessions
include "../settings.php"; // inlcudes config

switch ($_GET[friends]) { //allows multiple pages
default:
$id = $_SESSION['user_id'];
$get_username_value = get_username($id);
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$get_username_value' ");
$result= mysql_query($get)
or die("QUERY ERROR:<br />{$query}<br />" . mysql_error() ); //gets requests
while ($reqs = mysql_fetch_array($get))
{

echo ( "Friend Requests
$reqs[by_user] wants to be friends with you.
<a href='newfriends.php?friends=accept&user=$reqs[by_user]'>Accept</a><br/>
<a href='newfriends.php?friends=delete&user=$reqs[by_user]'>Delete</a>" ); //displays requests and shows accept delete links
}
break;

case 'accept': //accept page
if ($_GET[user]) { //get username
$get_username_value = get_username($id);
$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$get_username_value') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user] has been added as a friend and the request has been deleted" ); // echos the completion
}
break; //ends accept page

case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user]'s request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch
?>

as u can see by is now by_user

but im getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #8' at line 1

but i cant see any syntax errors

Inigoesdr
05-11-2008, 02:00 AM
Apparently get_username() is returning a DB resource instead of the field you expect.

runnerjp
05-11-2008, 10:49 AM
ok been playing around still lol and i seem to have got confused again...

ok so in my table i currenty have
________________________
| username | by_user |
|------------------------- |
| Emma | Bob |

but im getting the error

QUERY ERROR:
SELECT * FROM `friend_requests` WHERE `by_user` = Bob
Unknown column 'Bob' in 'where clause'

but bob is right there lol... and surly if it cant find bob it should display the oage still


<?php
session_start(); // starts sessions
include "../settings.php"; // inlcudes config

switch ($_GET[friends]) { //allows multiple pages
default:
$id = $_SESSION['user_id'];
$get_username_value = get_username($id);
$get = "SELECT * FROM `friend_requests` WHERE `by_user` = $get_username_value ";
$result= mysql_query($get) or die("QUERY ERROR:<br />{$get}<br />" . mysql_error() ); //gets requests
while ($reqs = mysql_fetch_array($get))
{

echo ( "Friend Requests
$reqs[by_user] wants to be friends with you.
<a href='newfriends.php?friends=accept&user=$reqs[by_user]'>Accept</a><br/>
<a href='newfriends.php?friends=delete&user=$reqs[by_user]'>Delete</a>" ); //displays requests and shows accept delete links
}
break;

case 'accept': //accept page
if ($_GET[user]) { //get username
$get_username_value = get_username($id);
$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$get_username_value') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user] has been added as a friend and the request has been deleted" ); // echos the completion
}
break; //ends accept page

case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user]'s request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch

?>

runnerjp
05-11-2008, 04:40 PM
woops i 4got 'quotes'


<?php
session_start(); // starts sessions
include "../settings.php"; // inlcudes config

switch ($_GET[friends]) { //allows multiple pages
default:
$id = $_SESSION['user_id'];
$get_username_value = get_username($id);
$get = "SELECT * FROM `friend_requests` WHERE `by_user` = '$get_username_value'";
$result= mysql_query($get) or die("QUERY ERROR:<br />{$get}<br />" . mysql_error() ); //gets requests

{

echo ( "Friend Requests
$reqs[username] wants to be friends with you.
<a href='newfriends.php?friends=accept&user=$reqs[username]'>Accept</a><br/>
<a href='newfriends.php?friends=delete&user=$reqs[namername]'>Delete</a>" ); //displays requests and shows accept delete links
}
break;

case 'accept': //accept page
if ($_GET[user]) { //get username
$get_username_value = get_username($id);
$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$get_username_value') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user] has been added as a friend and the request has been deleted" ); // echos the completion
}
break; //ends accept page

case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user]'s request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch

?>


now i im stuck,,, need some advie lol. ok the code doents pull the username of the person that wants to be accepted as a friend

i just get /members/newfriends.php?friends=accept&user=??
how would i pull the user name out so i can have

Name wants to be friends with you. Accept or Delete

and how would you go about making it so that a message is displayed when there is a user waiting to become a friend?

sorry i forgot to think this part through

runnerjp
05-12-2008, 12:15 PM
ok i have it working but i was thinking.... this is what i have...very basic but working :)


<?php session_start(); // starts sessions
include "../settings.php"; // inlcudes config
$id = $_SESSION['user_id'];
$get_username_value = get_username($id);
switch ($_GET[friends]) { //allows multiple pages
default:
$query = "SELECT * FROM friend_requests WHERE `by_user` = '$get_username_value' LIMIT 1";
if ($result = mysql_query($query)){
if (mysql_num_rows($result)) {
$array = mysql_fetch_assoc($result);
$puser = $array['username'];
$puser2 = $array['by_user'];
}}
?>
<?php echo ( "Friend Requests
$puser wants to be friends with you.
<a href='newfriends.php?friends=accept&user=$puser'>Accept</a><br/>
<a href='newfriends.php?friends=delete&user=$puser'>Delete</a>" ); //displays requests and shows accept delete links

break;

case 'accept': //accept page
if ($_GET[user]) { //get username
$get_username_value = get_username($id);
$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$get_username_value') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$get_username_value' "); // deletes friend request
echo ( "$_GET[user] has been added as a friend and the request has been deleted" ); // echos the completion
}
break; //ends accept page

case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$get_username_value'"); // deletes friend request
echo ( "$get_username_value's request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch
?>
but i need to make it go both ways,,, so if 1 become friends with 2 then 2 will also be friends with 1 but how !!!

TheShaner
05-12-2008, 03:33 PM
I explained this concept in my original reply to this thread. Please examine it carefully.

-Shane



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum