Monday, 14 May 2012

How can create paging in codeignator?

Pagination Class

CodeIgniter's Pagination class is very easy to use, and it is 100% customizable, either dynamically or via stored preferences.

Here we will tell create paging in codeignator.
  1. Create Model

class modelname extends CI_Model
{
function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

function get_all_posts($limit, $offset)
    {
        //get all entry
        $query = $this->db->get('entry', $limit, $offset);;
/****entry is table name *******/
        return $query->result();
    }
}

  1. Controller
class controllername extends CI_Controller
{
      function index()
    {
                  $data['pagetitle'] = "blog";//page name
                  $this->load->library('pagination');// call pagination lab…

         $config['base_url'] = base_url().'blog/index';
         $config['total_rows'] = $this->db->count_all('entry');
         $config['per_page'] = 5;
      $config['num_links'] = 2;
      //$config['use_page_numbers'] = TRUE;
      //$config['page_query_string'] = TRUE;
      $config['full_tag_open'] = '<p>';
      $config['full_tag_close'] = '</p>';
      $config['first_link'] = 'Start';
      $config['last_link'] = 'End';
      $config['next_link'] = ' > ';
      $config['prev_link'] = ' < ';
      $config['uri_segment'] = 3;
      //$config['last_tag_open'] = '<div>';
      //$config['last_tag_close'] = '</div>';
      //$config['next_tag_open'] = '<div>';
      //$config['next_tag_close'] = '</div>';
      $config['cur_tag_open'] = '<b>';
      $config['cur_tag_close'] = '</b>';
      //$config['display_pages'] = FALSE;

        $this->pagination->initialize($config);
        // $data['query'] = $this-> modelname ->get_all_posts();
        $this->load->view('blog/menu');
      $data=array('title'=>'Blog Listing');
      //this function will retrive all entry in the database
        $data['query'] = $this-> modelname ->get_all_posts($config['per_page'],$this->uri->segment(3));
                  $data['paging'] = $this->pagination->create_links();
                  $this->load->view('blog/index',$data);
    }

}
  1. view
<table width="80%" align="center">
     <tr>
                  <td>id</td>
            <td>Title</td>
            <td>Body</td>
            <td width="10%">Action</td>
        </tr>
                  <?php foreach($query as $post) { ?>
        <tr>
                  <td><?php echo $post->entry_id ; ?></td>
            <td><?php echo $post->entry_name;?></td>
            <td><?php echo $post->entry_body;?></td>
            <td>  <?php echo anchor('blog/edit/'.$post->entry_id, 'Edit', array('title' => 'Edit'));?>, Delet</td>
        </tr>
        <?php } ?>
        </table>
        <?php echo $paging?>  //call pagination function



No comments:

Post a Comment