pagination submit get value codeigniter - php

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

Related

Cannot access protected property CI_Pagination

Cannot access protected property `CI_Pagination
Trying to add submit button on last page of pagination currently it's on every page by default...on line 49 i have condition to check whether i am in last page or not, I want a submit button at the end of the pagination page
Code on which error is showing..please explain how to implement
if($this->pagination->cur_page >= ceil($this->pagination->total_rows / $this->pagination->per_page))
{
$isLastPage = true;
}else{
$isLastPage = false;
}
Pagination code
public function quizdisplay()
{
//echo $this->pagination->create_links();
//$this->load->library('pagination');
$config = array();
$config['base_url'] = 'http://localhost/xampp/cii/index.php/Questions/quizdisplay';
//$config['total_rows'] = 10;
$total_row = $this->quizmodel->record_count();
$config["total_rows"] = $total_row;
$config['per_page'] = 1;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}else{
$page = 1;
}
$data["results"] = $this->quizmodel->getQuestions($config["per_page"], $page);
$this->pagination->create_links();
$data["links"] = $this->pagination->create_links();
View data according to array.
$this->load->view("play_quiz", $data);
}
Both the codes are in same function

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;

CodeIgniter $this->pagination->create_links(); returning blank string

public function viewconsultant($id1='')
{
$id1 = $id1 ? $id1 : 1;
$pagenum=5;
$start=($id1-1)*$pagenum;
$users = $this->db->get_where('connections', array('user_id'=>97));
$pageall = count($users);
$config['total_rows']=$pageall;
$config['per_page']=$pagenum;
$config['num_links']=4;
$config['base_url']="localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant";
$config['use_page_numbers']=true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
}
The create_links() does not work well, it just displays a blank string. I tried this localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant/2, it can jump to second page, but not pagination links created blow the table. please help!!
The problem is that you are not creating the links..
$this->load->library('pagination');
$this->pagination->initialize($config);
// add the line below to your code
$data['links'] = $this->pagination->create_links();
// debug
// echo $data['links'];
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
Also, you can omit the first line in your method by doing the following:
public function viewconsultant($id1=1)
{
// line below no longer necessary
//$id1 = $id1 ? $id1 : 1;
}
Try this:
function viewconsultant($id1=1, $offset = 0){
$this->load->library('pagination');
$pagenum=5;
//$users = $this->db->where('connections', array('user_id'=>97))->limit('20', $offset);
$total = $this->db->where('connections', array('user_id'=>97));
$pageall = $total->num_rows();
//$users = $this->db->get_where('connections', array('user_id'=>97));
//$start=($id1-1)*$pagenum;
//$pageall = count($users);
$config['total_rows'] = $pageall;
$config['per_page'] = $pagenum;
$config["uri_segment"] = 3;
$config['num_links'] = 4;
$config['base_url'] = base_url()."/index.php/connections/viewconsultant/";
$config['use_page_numbers'] = true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $this->uri->segment(3));
$data['connections'] = $query->result();
$data['links'] = $this->pagination->create_links();
$this->load->view('profile_view',$data);
}
Now, just echo $links in your view file to get the pagination.

codeigniter pagination get url

I have a site developed in codeigniter and in a page I want to use pagination.
The problem is. my url now is smoething like that:
http://site/index.php/tee/view_tee/?id=2
This is my code into the controller:
$data['tee'] = $this->Tee_model->getTeeByUserId($this->input->get('id', TRUE));
$data['tee_like'] = $this->Tee_model->getLikeTeeByUserId($this->input->get('id', TRUE));
$data['user'] = $this->User_model->getUserById($this->session->userdata('id'));
$this->load->library('pagination');
$config['base_url'] = site_url().'/tee/view_tee/?id='.$this->input->get('id', TRUE);
$config['total_rows'] = count($data['tee']);
$config['per_page'] = 6;
$config['uri_segment'] = 3;
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
$data['page_links']=$this->pagination->create_links();
$this->pagination->initialize($config);
$this->load->view('view_tee_view',$data);
With this code when I click into my pagination link I have this url:
http://site/index.php/tee/view_tee/?id=2/6?id=2
If I click again I have this:
http://site/index.php/tee/view_tee/?id=2/6?id=2/6?id=2
This is my html:
echo $this->pagination->create_links();
foreach($tee as $t){
// I have three div per lines
}
add this...
$config['page_query_string'] = TRUE;
remove this...
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
[EDIT] WORKING EXAMPLE
$this->load->library('pagination');
$config['page_query_string'] = TRUE;
$config['base_url'] = site_url().'/tee/view_tee/?id='.$this->input->get('id', TRUE);
$config['total_rows'] = 200;
$config['per_page'] = 6;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
echo $this->pagination->create_links();

Categories