PDA

View Full Version : PHP Coding / SQL Databases Help Please!



ThisNameWillDo!
16-03-2010, 03:19 PM
Hey, I'm trying to code a system that shows what a user has done, for example, if a user has became friends with someone, or posted something on their profile. My coding is below:


<?
include "config.php";
if ($logged[username]) {
echo "<center><font class='normaltext'><u>Friend Updates</u><br /><br /></center><table border='0' cellspacing='2' cellpadding='0' class='normaltext'>";
$username = $logged['username'];
$getmates = mysql_query("SELECT * FROM friends WHERE to_user = '$username' AND confirmed = 'True'") or die(mysql_error());
$getmates2 = mysql_query("SELECT * FROM friends WHERE from_user = '$username' AND confirmed = 'True'") or die(mysql_error());

$number1 = mysql_num_rows($getmates);
$number2 = mysql_num_rows($getmates2);
if ($number1 == 0 && $number2 == 0) {
echo "You have no friend updates";
} else {
if ($number1 >= 1 && $number2 == 0) {
while($mates = mysql_fetch_array($getmates)) {
$getupdates = mysql_query("SELECT * FROM friendupdates WHERE username = '$mates[from_user]' ORDER BY id DESC") or die(mysql_error());

while($updates = mysql_fetch_array($getupdates)) {

$getfirstlast = mysql_query("SELECT * FROM users WHERE username = '$updates[username]'");
$firstlast = mysql_fetch_array($getfirstlast);
echo "<tr><td align='center'>On $updates[date] at $updates[time]<br /><a href='profile.php?user=$firstlast[username]'>$firstlast[firstname] $firstlast[lastname]</a> $updates[action]";
echo "<br /><br /></td></tr>";
}
}
} else {
if ($number1 == 0 && $number2 >= 1) {
while($mates2 = mysql_fetch_array($getmates2)) {
$getupdates2 = mysql_query("SELECT * FROM friendupdates WHERE username = '$mates2[to_user]' ORDER BY id DESC") or die(mysql_error());

while($updates2 = mysql_fetch_array($getupdates2)) {

$getfirstlast2 = mysql_query("SELECT * FROM users WHERE username = '$updates2[username]'");
$firstlast2 = mysql_fetch_array($getfirstlast2);
echo "<tr><td align='center'>On $updates2[date] at $updates2[time]<br /><a href='profile.php?user=$firstlast2[username]'>$firstlast2[firstname] $firstlast2[lastname]</a> $updates2[action]";
echo "<br /><br /></td></tr>";
}
}
}
}
}
echo "</table><br /><br />";
}
?>

So you know, to_user means the person that received the friend request, and from_user means the person that sent the friend request.
This is what happens with this code:

http://www.vincesgames.com/updatesscreenie.png

I have put circles around to show you what has happened. It has ordered them by usernames, but I don't want that, I want them to be in order of the date they happened (the date they got inserted into the database).

Does anyone have any idea how I can do this? I'm really confused. :S

Any help appreciated.

Thank you,
Vince.

LMS16
16-03-2010, 10:16 PM
When since you have "order by id desc" there, maybe try "ORDER BY date DESC"?

I didnt look through the code completely but maybe tht cud help?

Lew.

Joe!
16-03-2010, 10:36 PM
I find the easiest way to deal with dates and ordering entries in a database is by using a timestamp rather than putting the actual date and time in, then from that timestamp you can convert it into a date and time. Doing this will result in you being able to order properly :)

ThisNameWillDo!
16-03-2010, 11:53 PM
Hi, I put ORDER BY id DESC in all the $gets but it still didn't do it? :confused:

There's no point ordering by date, order by id does it fine.

Any solutions? Some of the coding must be wrong or in the wrong place?

Romanity
28-03-2010, 07:42 PM
Your $getmates query.... try ordering that by id DESC as well. And also the same with $getmates2. See if that works.
I also recommend you follow the advice of arranged by date.

Want to hide these adverts? Register an account for free!