i am using codeigniter using pagination but when i select the record per page it display the
data but when i click on next page button it will redirect to the 3 page by default we have 25 data field per page so it divide the data on pages by default value.
here is the controller
function search($page=0)
{
// selecting per page data
if ($this->input->post('per_page')!='') {
$recordperpage = $this->input->post('per_page');
$per = $this->input->post('per_page');
$page = $this->input->post('per_page');
echo $this->session->userdata('per');
}
else {
$recordperpage = 25;
}
$keyword = $this->input->post('keyword');
$result['front'] = $this->search_model->search($keyword,$page,$recordperpage);
$mypaing['total_rows']= $this->search_model->listing_num('*','tabel_name','flag',1,'keyword',$keyword);
$mypaing['base_url'] = base_url()."admin/h_search/search/";
$mypaing['per_page'] = $recordperpage;
$mypaing['uri_segment'] = 4;
$this->pagination->initialize($mypaing);
$result['recordperpage'] = $recordperpage;
$result['paginglink'] = $this->pagination->create_links();
$data['page_heading'] = "Doctor Search Result";
$data['contents'] = $this->load->view('admin/listing/search',$result,true);
$this->load->view('admin/template',$data);
}`enter code here`
model Model for getting record from search
function search($keyword,$page,$recordperpage)
{
$sql ="SELECT * FROM tabel_name WHERE flag = 1 AND keyword LIKE '%".$keyword."%'
LIMIT ".$page.",".$recordperpage."" ;
$result=$this->db->query($sql);
return $result;
}
function listing_num($select,$from,$where_colum,$where,$where_colum1,$where1)
{
$this->db->select($select);
$this->db->from($from);
$this->db->where($where_colum,$where);
$this->db->where($where_colum1,$where1);
$query=$this->db->get();
return $query->num_rows();
}
View
<div class="counter">
<input type="hidden" value="<?=$recordperpage?>" name="rpp_hidden" id="rpp_hidden" />
<select onchange="this.form.submit();" name="per_page" id="per_page">
<option value="25" <? if ($recordperpage == 25) { print 'selected=selected'; }?> >25</option>
<option value="50" <? if ($recordperpage == 50) { print 'selected=selected'; }?> >50</option>
<option value="75" <? if ($recordperpage == 75) { print 'selected=selected'; }?> >75</option>
<option value="100" <? if ($recordperpage == 100) { print 'selected=selected'; }?> >100</option>
</select> per page
</div>
</div>
when i select the data per post it will redirect to page 1 and shown data from 0 to 50 if i select record per page 50
thanks in advance
Where are you setting the offset if it’s not a search? Try adding this above your if statement. If there is no uri segment 4, like when you first load the page, it will set your offset to 0, if there is it will use the segment. Then if it’s a search your if statement will override it: