2

To be up front, I'm new to PHP/SQL programming. I've probably coded this wrong, or "the long way". To be honest, I don't know the difference. I'm just trying to make a basic web-chat room for a fun project. Currently, the messages that are displayed in the room are sorted by the timestamp in descending order. The newest messages are displayed on the top of the page. What I would like is to have the newest messages at the bottom of the page. I'm only displaying 30 messages at a time. I cannot figure out how to "flip" the messages so that the newest shows up at the bottom. (I hope this makes sense). Here's what I've been using to display the messages:

$query = "SELECT  * FROM roomdata ORDER BY timestamp DESC LIMIT 30 ";
$result = mysql_query($query);

if ($result) {   

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

// Check to see if the message was sent by a chatter
if ($row['sender']){
        $publicmessage= '<b>['.$row['sender'].']</b>  '.$row['message'];
}else{ 
// if not, it must have been sent by the server (ie: login message, server announcement, etc)
$publicmessage= $row['message'];
}
echo "$publicmessage <BR>";
    }
}

Can anyone point me in the right direction?

If I've not included enough information, I apologize - I'm new to all of this. Thank you in advance for your time!

6
  • 2
    Change your ORDER BY clause to be ASC Commented Mar 18, 2015 at 15:04
  • 1
    @John Conde that won't work because of the LIMIT. Commented Mar 18, 2015 at 15:06
  • 2
    I suggest you to avoid mysql_* functions, as they are deprecated. Look for mysqli or pdo extensions to connect to the db Commented Mar 18, 2015 at 15:07
  • 1
    Hmm. Does mysqli or pdo have an easier way to do this sort of thing? I don't mind learning something new if I need to. This is just a hobby and it's for fun. Commented Mar 18, 2015 at 15:12
  • No they doesn't, but this is a little off topic. To solve your problem just create an array with all your fetched comments and then reverse them using array_reverse() Commented Mar 18, 2015 at 15:18

1 Answer 1

1

You could of course put everything in an array in php and use array_reverse() to reverse the order (or take items of the end until it is empty...). For a pure sql solution, check out this question.

An alternative solution - if your browser requirements allow for it - would be to use flexbox to display the items and set:

.container {
    display: flex;
    flex-direction: column-reverse;
}

That way you can leave your php script as it is; the order in the html will be the same but on the screen it will be reversed.

You would also need to wrap your items in elements but a list would be more appropriate anyway than just plain text with line-breaks.

Sign up to request clarification or add additional context in comments.

4 Comments

I've been trying to do what you'd mentioned with the array_reverse() , but it's just been spitting things out in the same order. I'm clearly doing something wrong. I'm still researching though, in hopes that I can find a way to make that work. Thanks for the input!
@MikeRoberts If you post the modified code below your original question, we can all take a look.
I checked the link you provided, and that had the solution. Thank you so much!!
I would upvote your answer, but it won't let me since I'm new to the site and don't have enough rep yet. Once I get it, I'll come back and upvote it for you. Again, I really appreciate your super fast response.

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.