0

I'm working on a php/mySQL/ajax shoutbox and have run into a small snag. On the page I want the content to be loaded from oldest to newest, with the newest on the bottom. I also want to limit the output to reduce load times once the database starts to get a lot of data in it.

Here is the current code;

    <?php

include_once("../includes/db.php");
include_once("../includes/functions.php");

$q="SELECT tM.*, tC.char_name as username, tC.char_id as char_id
FROM shoutbox tM JOIN characters tC
ON tC.char_id=tM.char_id
ORDER BY shout_id DESC LIMIT 25";

db_Connect();

$result=mysql_query($q);

while($row=mysql_fetch_array($result))
{
    $classColor = getClassColor($row['char_id']);
    echo "<span class='".$classColor."'>".$row['username']."</span>: ",nl2br($row['shout_message'])."<br />";
}

mysql_Close();

?>

I have tried using while($row=array_reverse(mysql_fetch_array($result))) as well as $result = array_reverse(mysql_query($q)) but both return an error that array_reverse needs to be fed an array.

So far anything I have found on the web from the SQL side have all been answered "just use DESC or ASC accordingly."

4 Answers 4

1
$res = mysql_query($q);
$shouts = array();

while($row = mysql_fetch_assoc($res))
  $shouts[] = $row;
$shouts = array_reverse($shouts);

foreach($shouts as $shout) {
  // show them...
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want oldest to newest, and shout_id is auto increment just use

ORDER BY shout_id ASC LIMIT 25

You have DESC/ASC mixed up

1 Comment

ASC LIMIT 25 will return the first 25, so anyone posting after the first 25 never show up (this is how I discovered the problem in the first place, scratching my head wondering why new posts stopped showing up as soon as I switched to a different user)DESC LIMIT 25 will return the last, but in the wrong order because it effectively reverses the order of the entire table before selecting results. I've tried both ways and neither works, thus why I asked here.
1
$result=mysql_query($q);

unset($temp_array);

while($row = mysql_fetch_array($result))
    $temp_array []= $row;

$temp_array = array_reverse($temp_array);

foreach ($temp_array as $row)
{
    $classColor = getClassColor($row['char_id']);
    echo "<span class='".$classColor."'>".$row['username']."</span>: ",nl2br($row['shout_message'])."<br />";
}

2 Comments

Are you completely sure about $temp_array []= ...? Last time i checked, I believe this creates a false element as the last element of the array, since the last time you call it, mysql_fetch_array returns false.
And i upvoted yours since you had the patience to put in the "display" part ;)
0

Seems like a lit of work. I just used this:

$result = mysql_query($strQuery) or die(mysql_error());

while ($row = array_reverse(mysql_fetch_array($result))) {

$row['something'];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.