947740 Posted April 8, 2008 Share Posted April 8, 2008 I am making a message board, but I am having trouble listing the entries in the order I want them. Here is my current code to pull the info from the database: <?php include("includes\message_board.inc"); $cxn = mysqli_connect($host,$user,$password,$db); $sq1 = "SELECT * FROM messages"; $result = mysqli_query($cxn,$sq1); while($row = mysqli_fetch_row($result)) { echo"<tr><td>$row[0]</td><td>$row[1]</td><td>$row[3] $row[2], $row[4]</td><td>$row[5]</td><td>$row[6]</td></tr>"; } ?> Currently it lists the messages with the earliest on top, but I want the latest on top, seeing as I want the newest messages to be easily seen. I tried some code like this in place of the previous while statement.: for ($n=$affected;$n>0;$n--) { while($backwards_array[$n] = mysqli_fetch_assoc($result)) { $test = $backwards_array[$n]['Message']; } } I have tried many different variations, especiall of the $test placement. I believe the easy way to do this is to put each row element into an array and run through the array backwards, displaying the information in the correct order. However, I have not been able to figure out how to set up the array correctly. I would greatly appreciate any help you could give me with this. :-\ Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 The easy was is to have mysql order them for you. Order by a date posted/auto increment field in DESC order: $sq1 = "SELECT * FROM messages ORDER BY date_posted DESC"; Quote Link to comment Share on other sites More sharing options...
947740 Posted April 8, 2008 Author Share Posted April 8, 2008 Ah. I am not that familiar with MySQL, but I know the key things. Just out of curiosity, is there a somewhat easy way in PHP? Thank you very much for that piece of code! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 You could have read everything into an array and then used the array_reverse() function, or you could have read everything into an array and used a for loop starting with the last item of the array and working backwards. Much easier to get a database to sort though. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.