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 3 of 3

Thread: PHP Wall Help

  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP Wall Help

    Hey,

    I am having a problem with a wall comment system. I have a friend system and on the wall i want people only to see their friends comments or their own.

    Here is my code -

    PHP Code:
    $get mysql_query("SELECT * FROM wall ORDER BY id DESC") or die(mysql_error());

    $get_friends mysql_query("SELECT * FROM friends WHERE username='$username'");

    while(
    $friend_row mysql_fetch_assoc($get_friends))
    {

    $friends $friend_row['friendname'];

    $time_since mysql_query("SELECT * FROM wall WHERE user='$friends' OR user='$username'  ORDER BY time DESC");

    }

    $number mysql_num_rows($get);

    echo 
    "<table border='0'><tr><td width='10%' valign='top'>"

    include(
    "online.php");

    echo 
    "</td><td width='5%'><img src='images/line.png' /></td><td width='100%' valign='top'>";

    echo 
    "<form action='member.php' method='POST'><input type='text' size='75' name='message' maxlength='50' /> <input type='submit' name='submit' value='Post' /></form>";

    if (
    $number==0)
    {

    echo 
    "No Posts To Display";

    }
    else
    {

    echo 
    "<div id='wall'>";

    while (
    $get_row mysql_fetch_assoc($time_since))
    {

    $get_time $get_row['time'];
    $get_text $get_row['message'];
    $get_user $get_row['user'];

    $user_id mysql_query("SELECT * FROM users WHERE username='$get_user'");

    $user_row mysql_fetch_assoc($user_id);

    $user_row_id $user_row['id'];

    $diff $time $get_time// seconds
         
          
    switch(1)
          {
             case (
    $diff 60):
             
    $count $diff;
             if (
    $count==0)
                
    $count "a moment";
             else if (
    $count==1)
                
    $suffix "second";
             else
                
    $suffix "seconds";
             break;
             
             case (
    $diff 60 && $diff 3600):
             
    $count floor($diff/60);
             if (
    $count==1)
                
    $suffix "minute";
             else
                
    $suffix "minutes";
             break;
     
             case (
    $diff 3600 && $diff 86400):
             
    $count floor($diff/3600);
             if (
    $count==1)
                
    $suffix "hour";
             else
                
    $suffix "hours";
             break;
             
             case (
    $diff 86400 && $diff 604800):
             
    $count floor($diff/86400);
             if (
    $count==1)
                
    $suffix "day";
             else
                
    $suffix "days";
             break;
             
             case (
    $diff 604800 && $diff 2629743):
             
    $count floor($diff/604800);
             if (
    $count==1)
                
    $suffix "week";
             else
                
    $suffix "weeks";
             break;
             
             case (
    $diff 2629743 && $diff 31556926):
             
    $count floor($diff/2629743);
             if (
    $count==1)
                
    $suffix "month";
             else
                
    $suffix "months";
             break;
             
             case (
    $diff 31556926):
             
    $count floor($diff/31556926);
             if (
    $count==1)
                
    $suffix "year";
             else
                
    $suffix "years";
             break;
     
          }

    $get_full mysql_query("SELECT fullname FROM users WHERE username='$get_user'");

    $full_row mysql_fetch_assoc($get_full);

    $full_user $full_row['fullname'];

    echo 
    "<table border='0'><tr><td><font color='#3B5998'><b><a href='member.php?page=profile&id=".$user_row_id."'>".$full_user."</a></b></font>&nbsp;$get_text</font></td></tr><tr><td valign='top'><font size='1' color='#999999'>Posted: ".$count." ".$suffix." ago!<br /><hr size='1' width='540' /></font></td></tr></table>";


    I can only see my/my friends latest post. $username is the session.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This block is wrong:
    PHP Code:
    $get_friends mysql_query("SELECT * FROM friends WHERE username='$username'");

    while(
    $friend_row mysql_fetch_assoc($get_friends))
    {

    $friends $friend_row['friendname'];

    $time_since mysql_query("SELECT * FROM wall WHERE user='$friends' OR user='$username'  ORDER BY time DESC");


    What this says is:
    foreach of my friends query for my wall posts or their wall posts and order it by time.
    This isn't what you want, what you want is:
    Query for my wall posts or for wall posts created by users IN my friends ordered by time.

    What this will do is overwrite the $time_since with the LAST iteration of the friends. So if you have 1 wall post and your last friend has only 1 wall post, the result should be just these two wall posts even if your previous friend had 10.

    Replace that block with this:
    PHP Code:
    $time_since = @mysql_query("SELECT * FROM wall WHERE user = '$username' OR user IN (SELECT friendname FROM friends WHERE username = '$username') ORDER BY `time` DESC");
    if (!
    $time_since)
    {
        print 
    "An error has occured: " mysql_error();

    And post back your results. The above query could be broken into two queries if you like, or even use a join instead of a nested query. I would also change the $get and $number from:
    PHP Code:
    $get mysql_query("SELECT * FROM wall ORDER BY id DESC") or die(mysql_error()); 
    //...
    $number mysql_num_rows($get); 
    to
    PHP Code:
    $get mysql_query("SELECT count(id) FROM wall") or die(mysql_error());
    $number mysql_result($get0); 
    mysql_result itself is very slow (so you wouldn't use it in an iteration), but I'll bet that the count(id) is faster than a * when the wall starts getting larger and larger.
    If your intention is to only show the count for the user and their friends posts (the current count is all), than I would use the mysql_num_rows on the results of the $time_since since you need to query for all the records anyway.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Ryan55 (11-07-2010)

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much, worked perfectly


  •  

    Posting Permissions

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