CodeIgniter Tutorial: Use Pagination Class

Share on TwitterDigg This

MySQL DDL Query

Create a blogs table using sql below: 
CREATE TABLE `db_name`.`blog` (
    `blog_id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `title` VARCHAR( 200 ) NOT NULL ,
    `desc` TEXT NOT NULL ,
    `created` DATETIME NOT NULL ,
    `modified` DATETIME NOT NULL
) ENGINE = MYISAM

CodeIgniter Model

<?php
class blog extends Model
{
    function blog()
    {
        parent::Model();
        $this->load->database();
    }
    function count_blogs()
    {
        // Count total numbers of blog entries
        $query = $this->db->query('SELECT count(*) as count_blog FROM blogs');
        $result = $query->results();
        return $result[0]->count_blog;
    }
    function select_blogs($num, $offset)
    {
        // Fetch data from blogs table
        $query = $this->db->get('blogs', $num, $offset);
        $result = $query->results();
        return $result;
    }
}
?>

CodeIgniter Controller

<?php
class blog extends Controller
{
    function blog() {
        parent::Controller();
        $this->load->library(‘pagination’);
        $this->load->model(‘blog’);
        $this->load->helper(‘url’);
    }
    function bloglist() {
        $config['base_url'] = ‘http://your-site.com/index.php/test/page/’;
        $config['per_page']=10;
        $config['total_rows'] =$data['count_blog'];
        $this->pagination->initialize($config);
        $data['title']=’CodeIgniter Pagination Example’;
        $data['bloglist'] = $this->blog->select_blogs($config['per_page'],$this->uri->segment(3));
        $data['count_blog']=$this->blog->count_blogs();
        // This will generate your pagination links
        $data['page_links']=$this->pagination->create_links();
        $this->load->view(‘blog/index’,$data);
    }
}
?>

CodeIgniter View

<table>
    <?php for ($i=0; $i < count($bloglist); $i++) : ?>
    <tr>
        <td><?php echo $bloglist[$i]->title; ?></td>
    </tr>
    <tr>
        <td><?php echo $bloglist[$i]->desc; ?></td>
    </tr>
    <?php echo endfor; ?>
    <tr>
        <td><?php echo $page_links; ?></td>
    </tr>
</table>

Random

About prabin t p

Connect with me on Google+
This entry was posted in codeigniter, php. Bookmark the permalink.

2 Responses to CodeIgniter Tutorial: Use Pagination Class

  1. Milian says:

    Beautiful beautiful. On Facebook there’s an Art Philosophy group I benolg to and some of the women on there are making these using the AP cart and they are doing some lovely work too. I’m not sure I’m that ambitious, but you have once again inspired me!

  2. Hasan says:

    Well done article that. I’ll make sure to use it wiesly.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

*