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.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts

    PHP Explode issue

    Hi so I'm trying to make a friend system. In my head it all seemed possible, but for now I really can't figure it out. I'm trying to use explode and calling it it out with a while and an array. But the thing is, I want it to display their ID and then replace the ID into username. Then after that, print the username into an image because their avatars are stored remotely and not in the database.

    Here is my code.

    PHP:
    PHP Code:
    <?
    $result 
    mysql_query("SELECT * FROM members WHERE id='$id'");

    while(
    $row mysql_fetch_array$result )) {
    $friend_array $row['buddies'];

    $friend_result str_replace("|",", "$friend_array);

    echo 
    $friend_result;
    }
    ?>
    Then in the database, the buddies list looks kinda like this.

    Code:
    1|3|16|7|4

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,997
    Thanks
    15
    Thanked 234 Times in 234 Posts
    You're very unclear about what the problem is.
    PHP Code:
    str_replace("|",", "$friend_array); 
    You have an extra space behind the comma in that line. That could cause problems if there is no space in the data. What does the echo give you?
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Not really sure that this is the best way to go about doing this.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    I don't know your database structure and your avatar imagenames, but how I read it something along these lines should help you:

    PHP Code:
    <?
        $result 
    mysql_query("SELECT * FROM members WHERE id='$id'");

        while(
    $row mysql_fetch_array$result )) {
            
    // this was your friend_array
            
    $friends $row['buddies']; 
            
            
    // explode friends list (1|3|16|7|4) into array[1, 3, 16, 7, 4]
            
    $friend_array explode("|"$friend_array);
            
            
    // create empty result
            
    $friend_result "";

            
    // Start a for loop, looping through the full friend_array
            
    for($i 0$i<$friend_array.length$i++) {
                
    // For each friend ($i in friend_array) you'll need the name and avatar
                
    $result2 mysql_query("SELECT name, avatar FROM members WHERE  id='$friend_array[$i]'");
                while(
    $r mysql_fetch_array($result2)) {
                    
    // Retrieve name and avatar from result
                    
    $name $r['name'];
                    
    $avatar $r['avatar'];
                    
    // Add avatar image with name as alternative
                    
    $friend_result .= "<img src='{$avatar}' alt='{$name}' />";
                } 
    // end while-loop for friends
            
    // end for-loop for array    
        
    // end while-loop for profile
    ?>

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts
    @DrDOS Reason why I have the space is cuz I was just testing it out to see if I get any errors and it echos something like this.

    Code:
    1, 3, 16, 7, 4
    @myfayt I'm trying to go off what SMF does. They have pipelines and they use explode. I'm trying to see if I can do that too with my buddies list. I think that's the easiest way to have a friend system rather than having a data structure that has something like this.

    Code:
    CREATE TABLE `buddies` (
      `your_id` varchar(50) NOT NULL,
      `friend_id` varchar(50) default NULL,
      `status` varchar(10) default NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    
    INSERT INTO `buddies` VALUES('webmaster', 'admin', 'active');
    INSERT INTO `buddies` VALUES('admin', 'moderator', 'active');
    It's way too complicated because what if webmaster already added admin and there's a duplicate that goes
    Code:
    VALUES('admin', 'webmaster', 'active');
    Then it echo both the results and you'll see yourself on your own profile. I think the best way to go about with a friend system is to have pipelines and explodes.

    @Thyrosis Thanks. I'll have to try that out.

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Might be better to have a separate table for the buddy relationship. For example the table should just include the user ID and the buddies user ID. Thats just what I would do, it helps to make your application scalable and testable. I also dont like putting a bunch of ID's in one database column because I find it make my code become messy when inserting, updating and getting data. But hey thats just my opinion!

    Edit: Just seen you pretty much explained this, I would definitely go for this approach!
    Last edited by davidjones1990; 07-07-2013 at 12:51 PM. Reason: Being stupid!


  •  

    Posting Permissions

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