Hey, guys. I have a problem with pagination in my Codeigniter project. I am using the pagination library, and I have a search form. When I search for something, the results display on the page. When the resulting rows are more than the limit, I show the pagination links. But when I click on link number 2 to go to second page, the pagination links disappear. My search code is given below...
Controller function:
function search_branch()
{
$this->load->library('pagination');
$search_this=$this->input->post('inputsearchbranch');
$limit = 20;
$data['fields'] = array(
'branch_name' => 'Branch Name',
'city'=>'City',
'district'=>'District',
'tahsil'=>'Tahsil',
'Branch_manager'=>'Branch Manager'
);
$this->load->model('mod_user');
$searchbranch=$this->mod_user->search_branch($search_this,$limit);
$data['searchbranch'] = $searchbranch;
$config = array();
$config['base_url'] = site_url("ctl_home/search_branch/");
$config['total_rows'] = count($searchbranch);
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('view_searchbranch',$data);
}
My view:
<center>
<form class="userinfo" action="" method="post">
//table in which search record display
</form>
</center>
<?php if (strlen($pagination)): ?>
<div class="pagination">
<?php echo $pagination; ?>
</div>
<?php endif; ?>
Try to changing the "$config['uri_segment'] = 3;" to $config['uri_segment'] = 5;
And you are count the total count with the limited record. The total count value will be counted with all records.
function search_branch()
{
$this->load->library('pagination');
$search_this=$this->input->post('inputsearchbranch');
$limit = 20;
$data['fields'] = array(
'branch_name' => 'Branch Name',
'city'=>'City',
'district'=>'District',
'tahsil'=>'Tahsil',
'Branch_manager'=>'Branch Manager'
);
$this->load->model('mod_user');
$searchbranch=$this->mod_user->search_branch($search_this,false);
$config['total_rows'] = count($searchbranch);
$searchbranch=$this->mod_user->search_branch($search_this,$limit);
$data['searchbranch'] = $searchbranch;
$config = array();
$config['base_url'] = site_url("ctl_home/search_branch/");
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('view_searchbranch',$data);
}
& also use
$offset = $this->uri->segment(4);
Add new parameter to search_branch function and pass that to db..
$searchbranch=$this->mod_user->search_branch($search_this,$limit,$offset);
example Function($where,$limit,$offset)
$this->db->where($where);
$query = $this->db->get('mytable', $limit, $offset);
Related
I use codeigntier 3 pagination library.
It works, but not exactly how should.
First I will post my code.
controller setting
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$offset = $this->uri->segment(2);
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Model
class ItemsM extends CI_MODEL{
function allUsers($limit="", $offset ="")
{
$query = $this->db->select('*')
->from('items')
->limit($limit)
->offset($offset)
->order_by('creation_date', 'desc')
->get();
return $query->result();
}
public function countAll(){
return $this->db->count_all_results('items');
}
Route
$route['items/(:any)'] = 'items';
This pagination works, but the results Im getting are not correct.
On the first page/tab it shows items with this order
item 1 item 2 item 3 item 4 item 5 // ok
but when open page 2
I get next 5 results this way
item 3
item 4
item 5
item 6
item 7
Honestly I do not understand where is the problem, I someone know, please help. Thank you.
It seems your problem is with varialble $offset as you are passing $offset as your page number for eg. if your page number is 2 then it will skip 2 results but you need to skip 5 results. To solve this replace this code with you controller setting.
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$page = $this->uri->segment(2);
if($page > 1){
$offset = ($page-1) * $limit;
}else{
$offset = $page;
// Or you can set $offset = 0;
}
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Here i edited your variable $offset. Now you will get results as expected.
Codeigniter pagination does not work for me. It displays page numbers and first 5 search results, but when I click on the page '2' it loads 'search_nok' view with message "Please select your options". Could you please check my code below and help me to find the mistake.
Here is my controller:
public function search($offset = 0) {
$limit = 5;
$this->load->library('form_validation');
$this->load->model('model_x');
$this->form_validation->set_rules('country', 'Country','required');
if($this->form_validation->run()) {
$country = $this->input->post('country');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
$data['pagination'] = $this->pagination->initialize($config);
if ($this->model_x->did_search($country, $limit, $offset)){
$data["results"] = $this->model_x->did_search($country, $limit, $offset);
$this->load->view("search_ok",$data);
}
}
else
{
$data['message'] = 'Please select your options.';
$this->load->view("search_nok",$data);
}
}
Here is my view:
<?php
echo $this->pagination->create_links();
foreach($results as $row){
$country = $row['country'];
$city = $row['city'];
?>
<table>
<tr>
<td >
<b><?php echo $country; ?></b>
</td>
<td>
<b><?php echo $city; ?></b>
</td>
</tr>
</table>
<?php }?>
I think you are confused about Pagination. First of all, technically all of the pagination stuff:
<?php
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
should be in your controller, secondly, you should actually be limiting the results in your model as well, since you haven't shown your model I will come up with an example
$this->db->limit ($limit $offset //these will be defined in your controller)
$data = $this->db->get('whatever_table')->whatever_result_type.
return $data
you should define $limit in your controller and pass it over your your model function as a parameter that the function accepts
the controller should also have $offset and $limit defined
public function search($offset = 0) {
$limit = 5;
when you call your model make sure to pass these over
$this->model_x->did_search($country, $limit, $offset);
then instead of just $this->pagination->initialize($config);
do this(still in your controller remember):
$data['pagination'] = $this->pagination->initialize($config);
then echo pagination wherever you want it in your view
I'm using Codeigniter 2.2 and I try to build pagination ask Codeigniter guide and it is work perfectly for me when I used as url below
$pages_num: is the amount of pages for view.
http://localhost/Codeigniter2.1.4/public_html/page/$pages_num
But I get error when I add one argument for url
as code below I try to find it 3 weeks ago But I have not solution Please
Notes: number 2 is id categories and number 4 is amount of pagination view
http://localhost/Codeigniter2.1.4/public_html/cat/2/4
Here is my code in controller
$this->load->model('frontend/categories_m');
$count = $this->db->count_all_results('job');
$perpage = 2;
if ($count > $perpage) {
$this->load->library('pagination');
$config['base_url'] = site_url('cat/2');
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(3);
} else {
$this->data['pagination'] = '';
$offset = 0;
}
$this->db->limit($perpage, $offset);
$this->data['job_cat'] = $this->job_m->get_job();
$this->data['subview'] = 'cat';
$this->load->view('_main_layout', $this->data);
Here is for view:
<?PHP if ($pagination): ?>
<section class="pagination">
<?PHP echo $pagination; ?>
</section>
<?PHP endif; ?>
Please help
Sorry I can't post images here
With codeigniter pagination, the default link structure is as below
http://website/pages/getPagesViaAjax/3/5
Here pages is controller, getPagesViaAjax is function inside it and 3 is a parameter for category id and 5 will be the pagination count(i.e. offset)
in this case my pagination config is something like
$config['base_url'] = site_url('getPagesViaAjax/3');
$offset = $this->uri->segment(4);
So basically your offset will be the last argument and i.e. 5(available at segment 4) in this case. So prior to firing your query to model, please check the offset.
pagination is working but i dont know Why its showing only one data per page?? and i hv 20 data in my db and i can see only 6 data in 6pages :(
This is my controller
function view($page=0){
$config = array();
$config["base_url"] = base_url() . "index.php/view_expenses/view";
$config["total_rows"] = $this->emp_expenses_model->getTotalExpensesCount();
$config["per_page"] =1;
$this->pagination->initialize($config);
$this->data["results"] = $this->emp_expenses_model->getExpenses($config["per_page"], $page);
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
This is my model
function getTotalExpensesCount() {
return $this->db->count_all("emp_expenses");
}
function getExpenses($limit, $start) {
$this->db->limit($limit, $start);
$qry= $this->db->get("emp_expenses");
return $qry->result();
}
Any help thanks in advance :D
Try this:
function view(){
$config = array();
$config["base_url"] = base_url() . "index.php/view_expenses/view/".$this->uri->segment(3);
$config["total_rows"] = $this->emp_expenses_model->getTotalExpensesCount();
$config["per_page"] = 5;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$this->data["results"] = $this->emp_expenses_model->getExpenses($config["per_page"], $this->uri->segment(3));
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
function getExpenses($limit, $start = 0) {
$qry= $this->db->get("emp_expenses", $limit, $start);
return $qry->result();
}
$config["per_page"] = 5; means that you want to show 5 data per page, $config['uri_segment'] = 3; says what segment will hold the offset which you will be using in your query. $config["total_rows"] defines the total no. of rows in your table. $config["base_url"] defines the url the pagination will hold.
The data you are showing in your view is no way related to the pagination. Check your query and the offset you are getting. E.g. place echo $this->db->last_qeury();die; in function getExpenses() just before return statement.
You need to pass the uri segment like
$config["uri_segment"] = $last_seg_no;
Here $last_seg_no will be the last segment where you can find the page number.And need to pass the per page param also like
$config["per_page"] = 1;//As per your case
"dont know Why its showing only one data per page??" because of $config["per_page"] =1;.
Add the following,
$config['uri_segment'] = 3; // change based on ur url
Change the value from $config["per_page"] =1; to $config["per_page"] =3; to display 3 data per page.
Try this
$config['uri_segment'] = 3;
$config["per_page"] = 3;
Modifiy modal function like this
function getTotalExpensesCount() {
$this->db->select("count(*) as CNT");
$qry = $this->db->get("emp_expenses");
$result2 = $qry->row()->CNT;
return $result2;
}
i have faced this same problem. Now it solved by changing this
$config['uri_segment']
i have a page where books are to be display either with "grid" view or with "list" view.
So my current url is
http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/grid/8
and if i click on "list" link from my view page then my current page should load with only changing this part /grid/ to /list/ and other same.
i.e http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/list/8
I tried this but this is not helpful. Please help me to solve this issue.
inMyViewPage.php
<td width="10%" style="font-size:12px;color: red;">
<?php
echo "".anchor('#', 'Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4),' List', array('id' => ''))."";
?>
</td>
Instead of link if form submition solve my problem then please help me with that
Controller.php
function books()
{
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4);
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]=$this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
$viewType = ($this->uri->segment(4)) ? $this->uri->segment(4) : 'list';
if($viewType=='list')
{
$this->load->view('commonfiles/bookslistview',$data);
}
else {
$this->load->view('commonfiles/booksgridview',$data);
}
}
Need to add route in route.php like this
$route['viewallbooks/books/(:any)'] = "viewallbooks/books/$1";
Function books() shold contain 3 params with values by default
Below is more correct varian
function books($img_type = 'png', $view_type = 'list', $page = 0) {
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/'.$img_type.'/'.$view_type.'/'.$page;
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]= $this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
if($viewType=='list')
$this->load->view('commonfiles/bookslistview',$data);
else
$this->load->view('commonfiles/booksgridview',$data);
}
Do this one:
<?php echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/list/'.$this->uri->segment(5),' List', array('id' => '')).""; ?>
And no need to change anything in above controller.
You will have to do either:
Reload the page with the new url (grid) to render that template
OR:
Render both the grid and list view and then dynamically (with some css/javascript) hide content based on what you'll want to see.
OR:
Load the grid-view with AJAX and replacing the list with it.