Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-04-2004, 07:21 PM Frustrated beginner
Junior Talker

Posts: 2
Trades: 0
Hi, guys. I'm currently learning php and have come across a little problem. I set up this while loop to display all the info in my table, which works fine. Now how can I change this so that it only displays the last 5 rows? rather than displaying everything.

Thanks for your help in advance.


Alex

$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("couldn't select database");

$query = "SELECT * FROM ideas ORDER BY id";
$result = mysql_query($query)
or die ("couldn't execute query.");

while ( $row = mysql_fetch_array($result,MYSQL_ASSOC) )


{

echo "
<TR><TD bgcolor=999999><font face=verdana size=3><B>
<a href=ideas.php?idea={$row['id']}>{$row['title']}</a></b> -
<a href=more.php?topic={$row3['topicname']}>{$row3['topictext']}</a>
<TR><TD>
<font size=2>
<table><TR><TD valign=top>
<img src=./images/topics/{$row2['topicimage']} width=100 height=75><TD>
<font face=verdana size=2>{$row['toptext']}<BR><BR><center>
<font face=verdana size=2>
<a href=email.php?idea={$row['id']}><font color=999999>Email this to a friend</a> -
<a href=print.php?idea={$row['id']}><font color=999999>Printable version</a> -
<a href=comment.php?idea={$row['id']}><font color=999999>Comments</a> -
<a href=ideas.php?idea={$row['id']}><font color=999999>Read More</a>
</table><BR><BR>\n";

}
alex999 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-04-2004, 09:51 PM
techwench's Avatar
Code Monkey

Posts: 1,449
Name: Danalyn
Location: Dallas, TX
Trades: 0
Replace this:
Code:
$query = "SELECT * FROM ideas ORDER BY id";
Whith this:
Code:
$query = "SELECT * FROM ideas ORDER BY id LIMIT 5";
Or to show the last 5 from newest to oldest:
Code:
$query = "SELECT * FROM ideas ORDER BY id DESC LIMIT 5";
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by techwench; 10-04-2004 at 09:54 PM..
techwench is offline
Reply With Quote
View Public Profile
 
Old 10-04-2004, 10:33 PM
kisk's Avatar
Extreme Talker

Posts: 161
Location: skynet
Trades: 0
DESC LIMIT 5 would have been my choice as well...

I probabaly would then use a FOR statement to cycle thru the 5 rows...
kisk is offline
Reply With Quote
View Public Profile Visit kisk's homepage!
 
Old 10-05-2004, 06:37 AM
Junior Talker

Posts: 2
Trades: 0
That's fantastic. I spent ages messing around and the answer was so simple.

Thank you

Alex
alex999 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Frustrated beginner
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13483 seconds with 12 queries