View Single Post
Old 02-08-2008, 12:41 AM   #19 (permalink)
dylanfm
The Wanderer
 
dylanfm's Avatar
 
Join Date: Jan 2008
Location: Australia
Posts: 14
Thanks: 1
dylanfm is on a distinguished road
Default

I'll run through a way I do it, but I'm using postgres so the SQL is slightly different. This probably isn't the best way by any means, but it works for me.
  1. First you want to get your limit and offset sorted:
    PHP Code:
    $limit 3;
    $offset = (is_numeric($_GET['s'])) ? $_GET['s'] : null 
    This assumes that your start number is in the url as 's'
  1. Do your query. You can change your SQL syntax to MySQL if you need (really this is only replacing LIMIT $limit OFFSET $offset with the mysql equivalent).
    PHP Code:
    $query "SELECT id FROM blog_post ORDER BY created_at DESC";
    $query .= (is_numeric($start)) ? ' OFFSET '.$offset '' ;
    $query .= (is_numeric($limit)) ? ' LIMIT '.$limit '' ;
    $result pg_query($query);
    #Feed the results into an array called $blog_posts 
  1. I also want the total number of blog_posts.
    So, just do the above query without any OFFSET or LIMIT
    PHP Code:
    $number_of_posts = ..... 
  1. This is the code I use for the pagination thing. I'm not bothering with numbers at the moment, just next and previous. Numbers aren't hard to do though.
    PHP Code:
    <ul class="pagination">
        <?php 
            $date 
    = (is_numeric($mm) && is_numeric($yyyy)) ? $yyyy.'/'.$mm.'/' null ;
        if (
    $offset >= $limit): ?>
            <li><a href="/blog/<?=$date?>?s=<?php echo $offset $limit?>" title="View the previous page of blog posts">Newer posts</a></li>
        <?php endif ?>
        <?php if ((($offset $limit) < $number_of_posts) && ($number_of_posts count($blog_posts))): ?>
            <li><a href="/blog/<?=$date?>?s=<?php echo $offset $limit?>" title="View the next page of blog posts">Earlier posts</a></li>
        <?php endif ?>
    </ul>

I hope this is of some help. I wouldn't be suprised if I made a mistake above. Sorry about the weird 1,1,1 list!
Send a message via ICQ to dylanfm
dylanfm is offline  
Reply With Quote
The Following User Says Thank You to dylanfm For This Useful Post:
Orc (02-08-2008)