Jump to content

create friends list for profile site


runnerjp

Recommended Posts

Personally i would create a table ie "buddie"

fields

uID

UserID

FriendID

 

example data

 

uID | UserID | FriendID

1    |  1      |  3

2    |  1      |  2

3    |  2      |  3

 

User Table

UserID | name

1        | Bob

2        | Fred

3        | Tony

 

Bob is friends with Tony

Bob is friends with Fred

Fred is friend with Tony

Link to comment
Share on other sites

That is basically what I was getting at in a more refined manner

E.g.

 

Members Table

 

ID | Name | Friends | Email... etc.

1  | Bob  | 2, 3      | etc.

2  | Mark  | 1        | etc.

3  | Greg  | 1, 4      | etc.

4  | Meg  | 3        | etc.

 

Parse the ID's from friends field.

Link to comment
Share on other sites

the reason i would use another table is for better management, ie when deleteing a member and thus also wanting to remove his UserID from the system completely, updating users friend list field is a pain but deleting records is easy.. it really depends on how the system is going to grow!

 

i'll admit adding a 2nd table and linking it in on the searches can be a pain at times but again personally i find it easier in the end..

Link to comment
Share on other sites

It's a basic many to many relationship. Just have a table storing the IDs.

 

Tables:

CREATE TABLE users (
  user_id int(11) PRIMARY KEY NOT NULL auto_increment,
  username varchar(100) NOT NULL,
  # etc...
);

CREATE TABLE friends (
  user_id int(11) NOT NULL REFERENCES users(user_id),
  friend_id int(11) NOT NULL REFERENCES users(user_id)
);

 

Both of the IDs reference the users table.

 

Then to get user #34's friends you'd do:

SELECT u.* FROM friends AS f INNER JOIN users AS u ON f.friend_id = u.user_id WHERE f.user_id = 34;

 

 

This is also what MadTechie said.

Link to comment
Share on other sites

If you ever want to use the friends list in a SQL query, definitely use MadTechie's method. If the friend's lists is just a like a string containing CSV friend ID's, that list will almost be useless in an SQL query. It will require a PHP script to retrieve it, decode it and then put it back in the SQL query.

Link to comment
Share on other sites

ok guys i attacked it and its not 100% lol need little help

 

first off i get a blank page with this

 

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

if get_username($_SESSION['user_id']) { //checks user is logged in
switch get_username($_SESSION['user_id']) { //allows multiple pages
default:
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = 'get_username($_SESSION['user_id'])' "); //gets requests
while ($reqs = mysql_fetch_array($get))
{

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

case 'accept': //accept page
if ($_GET[user]) { //get username

$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('get_username($_SESSION['user_id'])' , 'get_username($_SESSION['user_id'])') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = 'get_username($_SESSION['user_id'])' "); // deletes friend request
echo ( "get_username($_SESSION['user_id']) 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_username($_SESSION['user_id']) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
echo ( "$_get_username($_SESSION['user_id'])'s request has been deleted" );
}
break;
} // ends switch

?> 

 

i belive its if get_username($_SESSION['user_id']) { //checks user is logged in

 

after this i will post whole script as i dont think its just right...but 1 problem at a time eh :P

Link to comment
Share on other sites

It doesn't look very efficient. I suppose the get_username() function queries the database, right? Nonetheless, you shouldn't use the username as the primary key, use the user ID instead. You'll also need to post how your tables look like, otherwise it's pretty difficult to tell you how to do it and optionally how to improve it. You can't run a function inside a string either. Interpolation only works with variables.

Link to comment
Share on other sites

ok i will paste all code....

 

ok so

 

CREATE TABLE `friend_requests` (
`id` int(10) NOT NULL auto_increment,
`username` varchar(225) NOT NULL default '',
`by` varchar(225) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `friends` (
`id` int(10) NOT NULL auto_increment,
`friendname` varchar(225) NOT NULL default '',
`username` varchar(225) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

 

and here is friends request

 

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


if ($_SESSION['user_id']){ //gets username
$username = htmlspecialchars get_username($_SESSION['user_id']); //friend
$by = ($_SESSION['user_id']); //you
$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
}
} else {
echo ( "You need to be logged in" ); //not logged in
}
?> 

 

this is done by following this link  <a href='index.php?page=friendrequest?user=$user'>Add as Friend</a> (THIS DOES NOT WORK ATM FOR SOME REASON)

 

 

then i have this to view friends

 

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

if ($_SESSION['user_id']) { //checks user is logged in
switch ($_SESSION['user_id']) { //allows multiple pages
default:
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = 'get_username($_SESSION['user_id'])' "); //gets requests
while ($reqs = mysql_fetch_array($get))
{

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

case 'accept': //accept page
if get_username($_SESSION['user_id']) { //get username

$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('get_username($_SESSION['user_id'])' , 'get_username($_SESSION['user_id'])') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = 'get_username($_SESSION['user_id'])' "); // deletes friend request
echo ( "get_username($_SESSION['user_id']) 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_username($_SESSION['user_id']) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
echo ( "$_get_username($_SESSION['user_id'])'s request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch

?> 

 

the functions are

<?php /**
 * 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;
	}
}
	// ------------------------------------------------------------------------

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

	function get_id ( $id )
{
	global $db;

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

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

		return $row->ID;
	}
	else {
		return FALSE;
	}
} ?>

 

atm this does not work and im abit stuck on where to go with it lol

Link to comment
Share on other sites

Comments below!

 

ok i will paste all code....

 

ok so

 

CREATE TABLE `friend_requests` (
`id` int(10) NOT NULL auto_increment,
`username` varchar(225) NOT NULL default '',
`by` varchar(225) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `friends` (
`id` int(10) NOT NULL auto_increment,
`friendname` varchar(225) NOT NULL default '',
`username` varchar(225) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

 

and here is friends request

 

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


if ($_SESSION['user_id']){ //gets username
$username = htmlspecialchars get_username($_SESSION['user_id']); //friend
$by = ($_SESSION['user_id']); //you
$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
}
} else {
echo ( "You need to be logged in" ); //not logged in
}
?> 

 

this is done by following this link  <a href='index.php?page=friendrequest?user=$user'>Add as Friend</a> (THIS DOES NOT WORK ATM FOR SOME REASON)

Check the input.. your adding the logged in user to himself!!!

i assume you mean

$username = htmlspecialchars(get_username($_GET['user'])); //friend

 

then i have this to view friends

 

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

if ($_SESSION['user_id']) { //checks user is logged in
switch ($_SESSION['user_id']) { //allows multiple pages
default:
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = 'get_username($_SESSION['user_id'])' "); //gets requests
while ($reqs = mysql_fetch_array($get))
{

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

case 'accept': //accept page
if get_username($_SESSION['user_id']) { //get username

$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('get_username($_SESSION['user_id'])' , 'get_username($_SESSION['user_id'])') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = 'get_username($_SESSION['user_id'])' "); // deletes friend request
echo ( "get_username($_SESSION['user_id']) 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_username($_SESSION['user_id']) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
echo ( "$_get_username($_SESSION['user_id'])'s request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch

?> 

 

 

switch on the userid is pointless the function concatenated into a string won't work..

$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('get_username($_SESSION['user_id'])' , 'get_username($_SESSION['user_id'])') "); // add to your friends list

 

the functions are

<?php /**
 * 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;
	}
}
	// ------------------------------------------------------------------------

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

	function get_id ( $id )
{
	global $db;

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

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

		return $row->ID;
	}
	else {
		return FALSE;
	}
} ?>

 

atm this does not work and im abit stuck on where to go with it lol

 

try cleaning it up and breaking the problems into smaller chunks and then ask for help..

 

add some debugging

ie

or die(mysql_error())

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.