Page 1 of 1

Reversing database reading

Posted: Mon Jul 01, 2002 11:44 am
by Z3r0Tol3ranc3
i have made a newspage and i want the last inserted news to stand on the top of the page. like this then:

news item 4
news item 3
news item 2
news item 1

but now it is like

news item 1
news item 2
news item 3
news item 4

pls help me out.

Posted: Mon Jul 01, 2002 11:45 am
by volka
what database?
for mysql you can use SELECT .... ORDER BY <your-field> DESC

Posted: Mon Jul 01, 2002 11:46 am
by Z3r0Tol3ranc3
can you Xplain that more precycly, i'm new at PHP
some info, don't know that it helps you out

include("config.inc.php");
mysql_connect("$host","$user","$pass");
mysql_select_db("$db");
$query="SELECT * FROM news";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$query="SELECT * order by news"; then or what??

Posted: Mon Jul 01, 2002 11:52 am
by volka
let's assume you have an auto-increment field called 'newsId' and it is the primary index of your table.
Then

Code: Select all

...
$result = mysql_query('SELECT topic FROM news', $conn); // or similar
while($row=mysql_fetch_row($result))
  myPrintTopic($row);
...
does what you've posted because the results are ordered by the primary index in ascend-direction (default)

Then reversing the order will be done by

Code: Select all

...
$result = mysql_query('SELECT topic FROM news ORDER BY newsId DESC', $conn);
while($row=mysql_fetch_row($result))
  myPrintTopic($row);
...
descending order of newsId

or you use a date/time-field. It's the same if you fill it with
INSERT INTO .... (...,datePost,...) VALUES (...,now(),...)

Try this...

Posted: Mon Jul 01, 2002 1:07 pm
by icesolid
Try and use this code:

Code: Select all

<?php
include("config.inc.php");
mysql_pconnect("$host", "$user", "$pass");
mysql_select_db("$db");

$sql = "SELECT * FROM news ORDER BY id DESC";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) &#123;
echo $row&#1111;"title"];
&#125;
?>
Remember that you must have an id tag which is set to primary and is also set to auto_increment. Doing so makes it a lot easier to search through and print out fields in your database.