Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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?

  • #2
    New Coder
    Join Date
    Sep 2012
    Posts
    12
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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

  • Users who have thanked webmiral for this post:

    Thuita Maina (09-07-2012)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Thanks Webmiral.

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    71
    Thanks
    0
    Thanked 8 Times in 8 Posts
    It's common just to use "limit 8" if you don't need a range such as "limit 2,10".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •