[SOLVED] ORDER by date DESC limit 10

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

[SOLVED] ORDER by date DESC limit 10

Post 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....
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post 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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

Code: Select all

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

?>
Should be that I think ...sorry
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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 :? .
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

thankyou very much, it works with duffs code.....MANY THANKS
Post Reply