Monday, December 3, 2012

How to create pagenation in Codeigniter?

 The follownig code clearly explains you how to cretae page nation in codeigniter.

Controller:
 Step1: declare two parameters to the function, where pagenation is required.
             public function function_name($start=1,$page='pagename')
 Step2: Pass the session variables to the view.
           $val['login']=$this->session->userdata('logged_in');
           $val['admin']=$this->session->userdata('user');
Step3: Intialize and check the start value.
             if(!is_numeric($start))
            {
                 $page = $start;
                 $start = 0;
             }
             else if($start == 1)
            {
                 $start = 0;
             }
Step4: Load the header
           $this->load->view('path to header');
Step5: Load the pagination library.
           $this->load->library('pagination'); 
Step6: Configured the pagination.
            $val["start"] = $start;
            $val["page_key"] = $page;
            $limit=20;
           $val['entry'] =$this->model->get($table,$limit,'sno',$start);
           $val["menu_tag"] = "function_name/1";
           $config['base_url'] = 'path';
            $config['total_rows'] = $this->model->total_rows($table);
           $config['per_page'] = 20;
          $this->pagination->initialize($config);
          $val["pagination"] = $this->pagination->create_links();
Step7: Load the view   
            $this->load->view('admin/pages/contact_info',$val);
Step8: we need to crete one more function in our controller for call current page
            public function page($currentpage)
           {
                  echo $currentpage;
                  echo $this->pagination->create_links();
             }
View:
Step9: The following two lines code added to your view where you want to put pagination.
              <?php $pagination = str_replace('">',"/$page_key\">",$pagination);?>
              <?php echo $pagination; ?>
Model:
Step10. write the following functions in database for fetch the data from database.
            function total_rows($table)
           {
               $query=$this->db->get($table);
               return  $query->num_rows();
            }
           function get($table,$limit,$order_by = "sno",$start = 0)
           {
              $query = $this->db->order_by($order_by, "asc");
               $query = $this->db->limit($limit);
                $query = $this->db->get($table);
                 return $query->result_array();
            }

 This is all about pagination.
Keep smiling......




            

No comments:

Post a Comment