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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Blog - Limit posts on page, next page

    Hi everyone. I'm a bit stuck on limiting posts on a page for a blog, and then have next page(page numbers). See the simple script below.

    PHP Code:
    echo "
    <div class='blog_post_container'>
        <div class='blog_post_title'>
        <h2>$title</h2>
        <div class='blog_post_date'>
            $date_posted</div></div>
            <div class='blog_post_content'>
            $contents</div>
    <hr class='blog_hr'>
    </div>
    "

    Contents of the database (my blog posts) are being pulled through to a single page.

    Not ideal if I have 20 posts as all on one page.

    Does anyone know how I can limit this to 10 on the page?, I would quite like to have some sort of next page links or page numbers after 10 posts?

    Even better if someone knows a youTube video or some other good resource to help?

    Don't knwo where to begin with this so any help most appriciated.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,551
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Paging if very very easy with MySQL!

    You just have to use the LIMIT keyword in you query.

    So something as simple as this:
    Code:
    $pageSize = 10; // or any other size
    $pageNumber = $_REQUEST("pageNumber"); // get page number from GET or POST
    if ( ! isset($pageNumber) ) $pageNumber = 1;
    $startLimit = ( $pageNumber - 1 ) * $pageSize;
    
    $sql = "SELECT ... FROM ... ORDER BY ... LIMIT $startLimit, $pageSize";
    ....
    $
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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