Page 1 of 1

[SOLVED] ORDER by date DESC limit 10

Posted: Fri Jan 23, 2004 9:21 pm
by dull1554
here's my code

Code: Select all

$topic_num = "1";
Print <<< EOT
<a href=index.php?l=add_topic.html>Add a Topic</a>
<br><br>
<table bordercolor='#000000' width=100% border=1 cellspacing=1>
<tr>
<td height=35 width=300>subject</td>
<td height=35 width=200>author</td>
<td height=35 width=20>replies</td>
<td height=35 width=20>views</td>
<td height=35 width=60>date</td>
<tr>
EOT;
while($topic_num<"10")
{
$query = mysql_query("SELECT * FROM topics ORDER by date DESC limit 10");
$array = mysql_fetch_array($query);
Print <<< EOT
<tr>
<td height=35>{$array['subject']}</td>
<td height=35>{$array['author']}</td>
<td height=35>{$array['replies']}</td>
<td height=35>{$array['views']}</td>
<td height=35>{$array['date']}</td>
<tr>
EOT;
$topic_num++;
}

Print <<< EOT
</table><br>
<a href=index.php?l=add_topic.html>Add a Topic</a>
EOT;
i am aware of the fact that the above code only returns the topic with the newest date 10 times, but what i cant figure out is what i have to do to get the top 10 newest posts and not just the newest one 10 times....

any input would be awesome....

Posted: Fri Jan 23, 2004 9:42 pm
by tsg

Code: Select all

<?php

$query = mysql_query("SELECT * FROM topics ORDER by date DESC limit 10"); 
$array = mysql_fetch_array($query); 
while ( $row = mysql_fetch_array($array) ) {
?>
<tr> 
<td height=35>{$array['subject']}</td> 
<td height=35>{$array['author']}</td> 
<td height=35>{$array['replies']}</td> 
<td height=35>{$array['views']}</td> 
<td height=35>{$array['date']}</td> 
<tr> 
<? } ?> 



?>
Try something like that

Posted: Fri Jan 23, 2004 10:03 pm
by dull1554
that returns

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\forum.test.php on line 21
not sure whats wrong, but also $row has no value, what should i define that $var as

Posted: Fri Jan 23, 2004 10:14 pm
by tsg

Code: Select all

<?php
while ( $row = mysql_fetch_array($query) ) { 

?>
Should be that I think ...sorry

Posted: Sat Jan 24, 2004 8:50 am
by DuFF
I think your code could be simplified by using a while statement, like this:

Code: Select all

<?php

Print <<< EOT
<a href=index.php?l=add_topic.html>Add a Topic</a>
<br><br>
<table bordercolor='#000000' width=100% border=1 cellspacing=1>
<tr>
<td height=35 width=300>subject</td>
<td height=35 width=200>author</td>
<td height=35 width=20>replies</td>
<td height=35 width=20>views</td>
<td height=35 width=60>date</td>
<tr>
EOT;

$query = mysql_query("SELECT * FROM topics ORDER BY date DESC limit 10") or die(mysql_error());

while ($array = mysql_fetch_array($query))
{
Print <<< EOT
<tr>
<td height=35>{$array['subject']}</td>
<td height=35>{$array['author']}</td>
<td height=35>{$array['replies']}</td>
<td height=35>{$array['views']}</td>
<td height=35>{$array['date']}</td>
<tr>
EOT;
}

Print <<< EOT
</table><br>
<a href=index.php?l=add_topic.html>Add a Topic</a>
EOT;

?>
Before, you had the MySQL query inside the loop. That means it executed 10 times :? .

Posted: Sat Jan 24, 2004 4:48 pm
by dull1554
thankyou very much, it works with duffs code.....MANY THANKS