twixter Posted February 5, 2008 Share Posted February 5, 2008 Hi, I have a slight prob. I got this script where it displays the list of the last 5 topics by date. problem is, it does show the last 5 topics, but the newest is at the bottom of the list. How can i make it show the last 5 entries, newest first? Thank you! Mae <? $row_count = mysql_num_rows(mysql_query("SELECT * FROM main where topic='statement' ORDER BY date ASC")) - 5; $result = mysql_query(" SELECT * FROM main where topic='statement' ORDER BY date ASC LIMIT ".$row_count.", 5") or die("error!"); if ($result) { while ($r = mysql_fetch_array($result)) { $id = $r["id"]; $topic = $r["topic"]; $date = $r["date"]; $title = $r["title"]; $area = $r["area"]; $os = $r["os"]; echo " • <a href=\"view.php?id=$id&topic=$topic&date=$date&title=$title&os=$os\">$title</a> <br> "; } } else { echo "No data."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/ Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Share Posted February 5, 2008 Change ASC to DESC. I am probably wrong, but it won't hurt to try. Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458258 Share on other sites More sharing options...
twixter Posted February 5, 2008 Author Share Posted February 5, 2008 heya maxudaskin, i tried that, changed one or both to "DESC" but it showed the very first 5 entries (the oldest ones). Thanks for the suggestion. it didn't hurt, dont worry. Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458280 Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 Go back to your original code, and get the records in reverse order...follow Example #1: http://us3.php.net/manual/en/function.mysql-data-seek.php Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458285 Share on other sites More sharing options...
awpti Posted February 5, 2008 Share Posted February 5, 2008 Change the ORDER BY to the table's auto_increment field. ORDER BY id DESC will give you the newest 5, in order from newest to oldest. Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458286 Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 ok...i interpreted that differently. twixter, do you want the oldest entries ordered newest to oldest or the most recent entries newest to oldest? Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458290 Share on other sites More sharing options...
twixter Posted February 5, 2008 Author Share Posted February 5, 2008 Hi awpti. I'll probably do that instead since it's the closest workaround. id is in auto_increment. thank you for your suggestion. rhodesa, i'll need the newest 5 entries by date, newest one on top. right now, what happens is like, 96, 97, 98, 99, 100 but i want it to be like 100, 99, 98, 97, 96. i'll probably use ORDER BY id for the meantime. Thanks too! Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458316 Share on other sites More sharing options...
twixter Posted February 5, 2008 Author Share Posted February 5, 2008 well, i tried using ORDER BY id and it still does the same thing. ??? Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458326 Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 <?php $query = "SELECT * FROM `main` WHERE `topic` = 'statement' ORDER BY id DESC LIMIT 5"; $result = mysql_query($query) or die("error!"); if(mysql_num_rows($result){ while ($r = mysql_fetch_array($result)) { $id = $r["id"]; $topic = $r["topic"]; $date = $r["date"]; $title = $r["title"]; $area = $r["area"]; $os = $r["os"]; echo " • <a href=\"view.php?id=$id&topic=$topic&date=$date&title=$title&os=$os\">$title</a> <br> "; } } else { echo "No data."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458335 Share on other sites More sharing options...
twixter Posted February 5, 2008 Author Share Posted February 5, 2008 Hi Rhodesa! I got this error: Parse error: syntax error, unexpected '{' in /home/XXXX/public_html/statements/index.php on line 21 where line 21 is if(mysql_num_rows($result){ Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458400 Share on other sites More sharing options...
ryeman98 Posted February 5, 2008 Share Posted February 5, 2008 Hi Rhodesa! I got this error: Parse error: syntax error, unexpected '{' in /home/XXXX/public_html/statements/index.php on line 21 where line 21 is if(mysql_num_rows($result){ You're missing a closing bracket: if(mysql_num_rows($result)){ Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458407 Share on other sites More sharing options...
twixter Posted February 5, 2008 Author Share Posted February 5, 2008 whoops! thanks ryman98! Thanks rhodesa! Quote Link to comment https://forums.phpfreaks.com/topic/89479-solved-putting-selected-values-in-order/#findComment-458423 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.