runnerjp Posted April 25, 2008 Share Posted April 25, 2008 how would i go about creating friends lists for my profile site? Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/ Share on other sites More sharing options...
Zhadus Posted April 25, 2008 Share Posted April 25, 2008 Each member have a friendslist array in the database and add other member ID #'s to the list. Simplistic design. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527224 Share on other sites More sharing options...
MadTechie Posted April 25, 2008 Share Posted April 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527227 Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 Or, you make a friends table in your database and have user_id and friend_id column, where you put the user's ID and the friend's ID. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527231 Share on other sites More sharing options...
Zhadus Posted April 25, 2008 Share Posted April 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527233 Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 Yeah, but that could get very messy and make a REALLY long row which would be a lot to parse, so it's easier and better database design practice to separate the tables. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527236 Share on other sites More sharing options...
runnerjp Posted April 25, 2008 Author Share Posted April 25, 2008 eeek lol so which one should i do user | friend_id just like that or what lol any tutorials on net? Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527242 Share on other sites More sharing options...
MadTechie Posted April 25, 2008 Share Posted April 25, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527246 Share on other sites More sharing options...
runnerjp Posted April 25, 2008 Author Share Posted April 25, 2008 im still abit confused...so i would do user_id | friends_id 1 | 2,3,5 2 | 1,3 3 | 1,2 Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527256 Share on other sites More sharing options...
MadTechie Posted April 25, 2008 Share Posted April 25, 2008 thats one way but not my way. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527263 Share on other sites More sharing options...
Daniel0 Posted April 25, 2008 Share Posted April 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527272 Share on other sites More sharing options...
dptr1988 Posted April 25, 2008 Share Posted April 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527275 Share on other sites More sharing options...
runnerjp Posted April 25, 2008 Author Share Posted April 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527287 Share on other sites More sharing options...
Daniel0 Posted April 25, 2008 Share Posted April 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527301 Share on other sites More sharing options...
runnerjp Posted April 25, 2008 Author Share Posted April 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527307 Share on other sites More sharing options...
runnerjp Posted April 25, 2008 Author Share Posted April 25, 2008 bmp Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527375 Share on other sites More sharing options...
Daniel0 Posted April 25, 2008 Share Posted April 25, 2008 Why are you putting the user id in the switch? Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527390 Share on other sites More sharing options...
runnerjp Posted April 25, 2008 Author Share Posted April 25, 2008 ow yer i should be getting the other users id...but how cna i do this... this script iv mashed up is poor and think it needs qucik look at and guidence would be gr8 Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527392 Share on other sites More sharing options...
runnerjp Posted April 26, 2008 Author Share Posted April 26, 2008 bmp Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-527670 Share on other sites More sharing options...
MadTechie Posted April 26, 2008 Share Posted April 26, 2008 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()) Quote Link to comment https://forums.phpfreaks.com/topic/102925-create-friends-list-for-profile-site/#findComment-528018 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.