pagination is not working in codeigniter - php

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?

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

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);

Related

pagination submit get value codeigniter

I using GET method for searching records. now when i have applied pagination i can't understand how to submit data to next pagination page link.
I have tried this answer. but it is just stacking page number in link segments.
example:
From
localhost/phc/search/results/0?s=ai&product_cat=all&post_type=product
To
localhost/phc/search/results/?s=ai&product_cat=all&post_type=product/12/24
so i didn't used that code.
And now my code is:
public function results() {
//pagination
$offset = $this->uri->segment(3);
$limit = 12;
//$offset = 0;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$config['first_url'] = base_url() .'search/results/0';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}
Tell what the best way to submit data in this case if not GET and how can i access this data.
Put offset in your url
localhost/phc/search/results?s=ai&product_cat=all&post_type=product&offset=123
and set these configs.
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
I changed your code but it's not tested at all.
public function results() {
//pagination
$offset = $this->input->get('offset') ? $this->input->get('offset') : 0;
$limit = 12;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
// set these mostly...
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}

Unable to configure pagination properly in Codeigniter

I'm new to Codeigniter pagination so i'm not able to configure it properly, that why i need your help guys.
This is my code so far
public function nearbay_get($idNearBay = null)
{
$config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay';
$config['total_rows'] = $this->Near_bays->count();
$config["per_page"] = 5;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['suffix'] = '?'.http_build_query($_GET, '', "&");
$config['use_page_numbers'] = TRUE;
$config['enable_query_strings'] = 'page=';
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$this->pagination->initialize($config);
$page = ($this->uri->segment(6))? $this->uri->segment(6) : 0;
$results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $page);
foreach($results as $data) {
echo $data->id_near_bay . " ------- " . $data->name . "<br>";
}
echo $this->pagination->create_links();
}
And this is the result if i visit http://mypage.si/rest/api/nearbay/nearbay
And if i click on any page number i get blank page and my url changes to http://mypage.si/rest/api/nearbay/nearbay?
So my question are. How to get pagination working? How to properly config pagination so i will be able to fetch results via ?page and ?per_page values trough url.. My final links should look like this http://mypage.si/rest/api/nearbay/nearbay?page=1&per_page=20
If you need any additional informations please let me know and i will provide... Thank you!
Here is answer, Hope it will help and url should be like this: http://mypage.si/rest/api/nearbay/nearbay/1
public function nearbay_get($idNearBay = null)
{
$config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay';
$config["per_page"] = 5;
$config["num_links"] = 3;
$config["uri_segment"] = 6;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['suffix'] = '?'.http_build_query($this->input->get(), '', "&");
$config['first_url'] = $config['base_url'] . $config['suffix'];
$page = ($this->uri->segment(6))? $this->uri->segment(6) : 0;
$offset = ($page == 0 ? 0 : ($page - 1) * $config["per_page"]);
$config['total_rows'] = $this->Near_bays->count();
$this->pagination->initialize($config);
$results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $offset);
foreach($results as $data) {
echo $data->id_near_bay . " ------- " . $data->name . "<br>";
}
//$current_page = $page == 0 ? 1 : $page;
//$start = $page == 0 ? 1 : (($page - 1) * $config["per_page"] + 1);
//$end = ($start + count($results) - 1);
//$total_page = ceil($config['total_rows'] / $config['per_page']);
echo $this->pagination->create_links();
}
For check uri_segment check the number. Currently is 6 try further number if you are not getting.

Codeigniter pagination not worrking returns 404

Hi I'd like some help please. I 'm having two controllers: movies.php and actors.php where I list all actors and all movies in them.
For example this is the method for listing all my movies
public function index() {
// count all movies
$total = $this->movie_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['movies'] = $this->movie_model->get();
// load the view
$this->view('movies/index');
}
and for listing all actors
public function index() {
// count all actors
$total = $this->actor_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['actors'] = $this->actor_model->get();
// load the view
$this->view('actors/index');
}
I have set my routes like this
$route['default_controller'] = "movies";
$route['404_override'] = '';
$route['movies'] = 'movies/index';
$route['actors'] = 'actors/index';
And the urls are like this
h**p: //localhost/www/task/public/ // for movies (default controller)
h**p: //localhost/www/task/public/actors // for actors controller
The problem is when I try to click a panination link to get the next records in each controller I get a 404 error. I have tried to change my config settings in pagination but no luck.
Any help would be appreciated.
I have link here very help full for pagination and sortable table links this may help.
http://forum.codeigniter.com/thread-1198.html
Change $config['base_url'] = site_url($this->uri->segment(1) . '/'); to,
$config['base_url'] = base_url().'movies/index';
and
$config['base_url'] = base_url().'actors/index';
And $config['uri_segment'] = 3;

How to write Segment in pagination library code igniter

Mysql Segment ?? not understood properly
in pagination...
Here is my code:
$data['query']= $this->db->select('auto_id,title,image,thumb')
->get('gallery',$config['per_page']=12,$this->uri->segment(3));
where per_page=12
try to use like this->
function records($sort_by = 'rpd_id', $sort_order = 'asc', $offset = 0) {
$limit = 10;
$data['results'] = $this->rcbs_Model->get_all_data_from_request($limit, $offset, $sort_by, $sort_order);
$config = array();
$config['base_url'] = base_url("rcbs/records/$sort_by/$sort_order");
$config['total_rows'] = $this->rcbs_Model->count_records();
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$this->load->view('booking_data', $data);
}
here $sort_by = 'rpd_id' (rpd_id)is the default table column by which
data will be sorted.
"get_all_data_from_request" is the function by which you will get all
data from your table using select query.
base_url("rcbs/records/$sort_by/$sort_order"); rcbs -Controller name
'records'->function name itselft
$this->rcbs_Model->count_records() count all the records from the
table.

Codeigniter security check if uri is number, because of pagination

One thing in the security of codeigniter is checking if the uri is a number or something that we expect!
Here is the code:
$this->load->helper('form');
$this->load->library('pagination');
$config['permitted_uri_chars'] = '0-9';
$config['base_url'] = '/member/index';
$config['per_page'] = 5;
$config['num_links'] = 10;
$query_not_voted_rows = "SELECT p_id FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').")";
$config['total_rows'] = $this->db->query($query_not_voted_rows)->num_rows();
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
if($this->uri->segment(3) == '')
{
$segment_url = 0;
}else{
$segment_url = $this->uri->segment(3);
}
$query_not_voted = "SELECT * FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').") LIMIT ".$segment_url.", ".$config['per_page'];
But if now someone enters in uri segment: "kdkdkdd", then the sql breaks, because $segment_url is kdkdkdd and not the number!
So the question is, how to escape this?
This is my simple solution:
if($this->uri->segment(3) == '')
{
$segment_url = 0;
}else{
if(!is_numeric($this->uri->segment(3))){
redirect('404');
}else{
$segment_url = $this->uri->segment(3);
}
}
But if somebody have any better idea or tutorial on this please...
nice job =)
answer 1 +
<?
$cadena = "sdfio9874673o4h784";
for ($i=0; $i<strlen($cadena); $i++) {
if (is_numeric($cadena[$i])) {$cadena2.=$cadena[$i];}
}
echo $cadena2;
?>
= nice code

Categories