CodeIgniter Forums
Pagination for kamash - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Pagination for kamash (/thread-49367.html)



Pagination for kamash - El Forum - 02-16-2012

[eluser]Shiju S S[/eluser]
controller function for pagination, be careful with the baseurl. The data begining from category are for my needs, you will only need to pass: perpage and limit

1. The $total pages load your total count
2. $perpage,$this->uri->segment(3)

Code:
public function searchboxpagination($pageno)
{
  $category=$this->session->userdata('category');
  $subcategory=$this->session->userdata('subcategory');
  $country=$this->session->userdata('country');
  $state=$this->session->userdata('state');
  $district=$this->session->userdata('district');
  $city=$this->session->userdata('city');
  
  // get row count based on the query before using the limits
  $totalrows = $this->sales_exe_details_db->searchcount($category,$subcategory,$country,$state,$district,$city);
  $perpage=4;
  
  // fetch rows for pagination --- items is the result it is passes to the view
  $data['items'] = $this->sales_exe_details_db->searchproducts($category,$subcategory,$country,$state,$district,$city,$perpage,$this->uri->segment(3));
  

  $this->load->library('pagination');
     $config['base_url'] = base_url().'frontpage/searchboxpagination';
     $config['total_rows'] = $totalrows;
     $config['per_page'] = $perpage;
      
  $this->pagination->initialize($config);
  
  $data['title'] = 'quickbharath.com : Search Results';
  $this->load->view('frontarea/header', $data);
  $this->load->view('frontarea/searchbox', $data);

  $this->load->view('frontarea/searchresult_view', $data);
  
}

Define routes now:
Code:
$route['frontpage/searchboxpagination/(:any)'] = "frontarea/frontpage/searchboxpagination/$1";
$route['frontpage/searchboxpagination'] = "frontarea/frontpage/searchboxpagination/$1";
The two routes are required since the controller calls it without parameter for the first time. We can think about removing it later.

Define model functions: Again this is for my needs. The first function sets the limit and no of records.
The second is for just counting only. You can do it in your own way.

Code:
function searchproducts($category,$subcategory,$country,$state,$district,$city,$limit = NULL, $offset = NULL)
{
  $this -> db -> select('*');
  //$this -> db -> from('listeditems');
  if($category!="ALL")
   $this -> db -> where('category = ' . "'" . $category . "'");
  if($subcategory!="ALL")
   $this -> db -> where('subcategory = ' . "'" . $subcategory . "'");
  if($country!="ALL")
   $this -> db -> where('country = ' . "'" . $country . "'");
  if($state!="ALL")
   $this -> db -> where('state = ' . "'" . $state . "'");
  if($district!="ALL")
   $this -> db -> where('district = ' . "'" . $district . "'");
  if($city!="ALL")
   $this -> db -> where('city = ' . "'" . $city . "'");
  
  $this->db->order_by("planorder", "asc");
  $this->db->limit($limit, $offset);
  

  return $this->db->get('listeditems');

}
function searchcount($category,$subcategory,$country,$state,$district,$city)
{
  $this -> db -> select('*');
  //$this -> db -> from('listeditems');
  if($category!="ALL")
   $this -> db -> where('category = ' . "'" . $category . "'");
  if($subcategory!="ALL")
   $this -> db -> where('subcategory = ' . "'" . $subcategory . "'");
  if($country!="ALL")
   $this -> db -> where('country = ' . "'" . $country . "'");
  if($state!="ALL")
   $this -> db -> where('state = ' . "'" . $state . "'");
  if($district!="ALL")
   $this -> db -> where('district = ' . "'" . $district . "'");
  if($city!="ALL")
   $this -> db -> where('city = ' . "'" . $city . "'");
  

  $result=$this->db->get('listeditems');
  
  return $result->num_rows();

}

Last - The view:
Two things to notice here are
1. It takes the result as row from the parameter $data passed
if($items)
{
foreach($items->result_object() as $row)
{
2. Shows the page links

<?php echo $this->pagination->create_links(); ?>

Code:
<link href="<?Php echo base_url()?>css/globalstyle.css" rel="stylesheet" type="text/css" />

<div id="content_centre">

<div class="titile_bar">Search Results</div>

&lt;?php // Show platinum only
    
  
   if($items)
   {
    foreach($items->result_object() as $row)
    {
       $logo=$this->sales_exe_details_db->getlogo($row->prdid);
     $prdid=$row->prdid;
     $planid=$row->planid;
    
     if($planid=="PLATINUM")
      $color="content_centre_smallp";
     else if($planid=="GOLD")
      $color="content_centre_smallg";
     else    
      $color="content_centre_smalls";
    
?&gt;
    
<div id="&lt;?php echo $color; ?&gt;">

<div class="titile_bar">  &lt;?php echo $row->title; ?&gt; </div>
<table width="610" border="0" cellspacing="4" cellpadding="4">
  <tr>
    <td>
    
<h4> We do business in : </h4>
<h5>&lt;?php echo $row->category; ?&gt; : </h5>
&lt;?php echo $row->subcategory; ?&gt;

    </td>
    
    <td><div id="add_small">
<a href="&lt;?Php echo base_url() ?&gt;frontpage/productdetailsview/&lt;?Php echo $prdid ?&gt;"><img src="&lt;?Php echo base_url() ?&gt;uploads/logo/&lt;?Php echo $logo; ?&gt;" align="left" width="136" height="60" /></a> <br />
</div>
</td>
  </tr>
</table>

</div>
&lt;?php } } ?&gt; &lt;!-- END OF PLATINUM--&gt;
<table width="1010" border="0" cellspacing="4" cellpadding="4">
  <tr>
    <td width="978" align="center" valign="middle">&lt;?php echo $this->pagination->create_links(); ?&gt;</td>
  </tr>
</table>
</div>