Reputation: 956
I am working in CodeIgniter. I want to create pagination for dynamic record. So I have write code like below:
$config = array();
$config["base_url"] = base_url() . "employer/search_user";
$config["per_page"] = 3;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$query = "select sn.*,u.first_name,u.last_name,u.email,u.phone,group_concat(DISTINCT s.skill) as sk,group_concat(DISTINCT c.name) as ct from
users as u
left join snapshot as sn on sn.user_id = u.user_id
left join industry as ind on ind.ind_id = sn.industry
left join city c ON(FIND_IN_SET(c.city_id, sn.current_location) > 0)
left join skill s ON(FIND_IN_SET(s.skill_id, sn.skill) > 0)
where ".$where." u.group_id = 3 group by u.user_id limit ".$config["per_page"];
$data['users'] = $this->admin_model->query($query);
$row = count($data['users']);
$config["total_rows"] = $row;
$config['num_links'] = $row;
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data['main_content']="employer/search_user";
$this->load->view('employer/template', $data);
Here, I got 3 records in page but pagination link is not display. I have include pagination library in constructor. I did not get anuthing in $data["links"]
this variable.
So where I have to correct my code? What is wrong in my code?
Upvotes: 0
Views: 122
Reputation: 1641
You need to use LIMIT
and OFFSET
in your query. For example
$config['per_page'] = 3;
$config['uri_segment'] = 4; // This is the current page
$offset = 0;
if ($this->uri->segment($config['uri_segment']) != "") {
$offset = $this->uri->segment($config['uri_segment']);
}
$limit = $config['per_page'];
$sql = "SELECT `field_name` FROM `table_name` WHERE `table_name`.field_name = ?";
if($limit != "") {
$sql .= "LIMIT $limit ";
}
if($offset != "") {
$sql .= "OFFSET $offset";
}
$result = $this->db->query($sql, array($field_data));
Hope this helps
Upvotes: 1
Reputation: 93
For $config["total_rows"] = $row; You get only 3. Because You are setting limit in the query. Kindly write another query for total rows without limit.I think this will solve Your problem.
Below is my pagination code
$config = array();
$config["base_url"] = base_url() . "crm/crm/contactmgmt/modid/" . $modid;
if($this->input->post('Search')=='Search'){
$data['pn']=$data1['pn']=$this->input->post('sel_prod');
//echo "hello";
}
$config["total_rows"] = $this->Crm_model->record_count_unsubinst();
$config["per_page"] = 6;
$this->load->config('pagination');
$config["uri_segment"] = 6;
$config['enable_query_strings']='true';
$data['activeTab'] = "View";
$this->pagination->initialize($config);
$page = ($this->uri->segment(6)) ? $this->uri->segment(6) : 0;
$data['query'] = $this->Crm_model->listUnsubscribedInstn($config["per_page"], $page);
$data["link"] = $this->pagination->create_links();
$data["links"]=$data["link"];
$data["cnt"]=$config["total_rows"];
$data["sno"]=$this->uri->segment(6);
Upvotes: 0