FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP and MySQL Order - Help.





Horizon
I've been studying PHP for a good while and I've actually managed to create my own, crude forums from scratch. But now the problem I'm having is ordering the topics in a certain way and have the newest topics come first. So here's the chunk of code I use to first display the results from the database:

Code:

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("database", $con);


$result=mysql_query("SELECT * FROM topic");

while ($row = mysql_fetch_array($result))
{
echo "Topic name: " . $row['name'] . " ";
}

mysql_close($con);
?>


Within the database, I've auto_incremented the row called 'id', meaning when a new row is added, the 'id' number will increase on its own. And the only other row is called 'name' which is the topic name. So is there any way to make it show the newest topic at the top instead of adding it at the bottom of the webpage?

If you're confused at all about the webpage, here's an example:

Quote:

Topic name: Hello


and if a new topic is added, let's say it's called 'Cheese', it turns out as:

Quote:

Topic name: Hello
Topic name: Cheese


So I need it to do the opposite of this. And.. if you're still confused, then here's the last example. This is what I want it to show:

Quote:

Topic name: Cheese
Topic name: Hello
Diablosblizz
Couldn't you add a date then order it by date desc to make the latest date come first?
Peterssidan
You can change your SQL query to:
Code:
SELECT * FROM topic ORDER BY id DESC
In this way the result will be ordered with the largest id first and the smallest last.
rvec
Diablosblizz wrote:
Couldn't you add a date then order it by date desc to make the latest date come first?
did you read the part about the id? As Peterssidan said, you can just use that.
Diablosblizz
I posted the exact same thing, just didn't show how to do it. That's basically what I meant.
Horizon
Peterssidan

Thanks very much guys, it worked perfectly.

Peterssidan: Thank you for saying specifically how to do it ^^.

Diablosblizz: The date seems to work better.. When I tried to remove corrupt posts, the id's got all messed up and the posts wouldn't show in the correct order. By having it set for the date and everything, it put everything back where it's supposed to be.

So everything's working fine now, thanks.

This topic can be closed now ^^
Related topics
A very good PHP MySQL Tutorial
Link PHP/Mysql with Outlook Express
php-mysql source code
portable php+mysql
PHP, MySQL...
ASP +PHP+MySQL Tutorials
what is your php mysql apache installer package
Need some php/mysql dlls
The Basics (php, mysql etc)
What is useful way in this php+mysql problem?
PHP/Mysql - beginner!
Setup php,mysql and others...
PHP/mySQL MJguestbook Help
php, mysql and iis5
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.