Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-07-2012, 07:32 AM   PM User | #1
Thuita Maina
New Coder

 
Join Date: Sep 2012
Posts: 77
Thanks: 10
Thanked 0 Times in 0 Posts
Thuita Maina is an unknown quantity at this point
Question How to Display the latest 8 stories' title

I have created a sidebar that is supposed to show the titles of the latest 8 stories. I however have the premonition that the sidebar will only display the titles of rows 0 to 8 instead of displaying the last eight regardless of row number. Below is the php code that I wrote for displaying the titles with the code in red triggering my premonition. Will the LIMIT statement really work to achieve my objective?

Code:
<?php 
 
// Include database connection settings
include('config.inc');
  
$result = mysql_query("SELECT * FROM stories order by datetime ASC limit 0,8");    
  
while($row = mysql_fetch_assoc($result))  
          {                 
               $id=$row['id']; 
               echo '<h3><a href="stories.php?id='.$id.'">' .$row['title'] . '</a></h3>';
               echo 'By ' . $row['names'] . '';
                 
          }  
     
  ?>
Again I ask, will the limit statement only select the last eight or the rows 0 to 8?
Thuita Maina is offline   Reply With Quote
Old 09-07-2012, 07:54 AM   PM User | #2
webmiral
New Coder

 
Join Date: Sep 2012
Posts: 12
Thanks: 0
Thanked 2 Times in 2 Posts
webmiral is an unknown quantity at this point
Hi,

The first 8 story:
Code:
SELECT * FROM stories order by datetime ASC limit 0,8
The last 8 story:
Code:
SELECT * FROM stories order by datetime DESC limit 0,8
webmiral is offline   Reply With Quote
Users who have thanked webmiral for this post:
Thuita Maina (09-07-2012)
Old 09-07-2012, 08:05 AM   PM User | #3
Thuita Maina
New Coder

 
Join Date: Sep 2012
Posts: 77
Thanks: 10
Thanked 0 Times in 0 Posts
Thuita Maina is an unknown quantity at this point
Thanks Webmiral.
Thuita Maina is offline   Reply With Quote
Old 09-07-2012, 02:09 PM   PM User | #4
vroom
New Coder

 
Join Date: Sep 2012
Posts: 71
Thanks: 0
Thanked 8 Times in 8 Posts
vroom is an unknown quantity at this point
It's common just to use "limit 8" if you don't need a range such as "limit 2,10".
vroom is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 03:38 AM.


Advertisement
Log in to turn off these ads.