...

View Full Version : PHP Wall Help



Ryan55
11-06-2010, 01:46 PM
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 -


$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.

Fou-Lu
11-06-2010, 05:26 PM
This block is wrong:


$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:


$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:


$get = mysql_query("SELECT * FROM wall ORDER BY id DESC") or die(mysql_error());
//...
$number = mysql_num_rows($get);

to


$get = mysql_query("SELECT count(id) FROM wall") or die(mysql_error());
$number = mysql_result($get, 0);

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.

Ryan55
11-07-2010, 01:00 AM
Thanks so much, worked perfectly ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum