Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts

    adding user as a friend

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

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Code:
    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.

    PHP Code:
    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

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    that still confuses me lol

    any more help lol

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    What exactly confuses you about it? Its pretty straight forward. Perhaps you should read some books or do some tutorials.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    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

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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...

  • #7
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    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 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
    }

    ?>
    this will work just grabbing the username of the friend

  • #8
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    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 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

  • #9
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    I realised how stupid my reply was.... so I deleted it.

    Sorry.

  • #10
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    ok chnaged it to this

    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
    }

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

  • #11
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    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 Code:
    <?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')";
      
    $resultmysql_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

    PHP Code:
    /**
         * 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 ) == )
            {
                
    $row $db->getRow $query );
                
                return 
    $row->Username;
            }
            else {
                return 
    FALSE;
            }
        } 

  • #12
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    You need to add the $id variable when calling the function

  • #13
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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
    PHP Code:
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #14
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    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
    PHP Code:
    <?
    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' "); 
    $resultmysql_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

  • #15
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Apparently get_username() is returning a DB resource instead of the field you expect.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •