How to use pagination in codeigniter with segments? - php

I have one problem in Codeigniter pagination with segments. I need to pass segments with page number.
Example: http://www.mysite.com/app/controller/method/param1/param2/page_number
<?php
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/welcome/par1/para2/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
?>

Try using the URL helper function current_url() as your base url.
From the CI documentation (http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html):
current_url() -
Returns the full URL (including segments) of the page being currently viewed.
So, in your situation, the controller would look like this:
$this->load->library('pagination');
$config['base_url'] = current_url();
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
This method should allow you to have as many parameters as you need.

Related

view within view with pagination in codeigniter

I have a view named "home.php" with a load button. When I click the button I am loading another view having pagination using ajax.
print $this->load->view('/layout/superadmin/email_listing', $this->_data, true);
For pagination in email_listing view, I have below code in model:
$config['base_url'] = site_url().'/home';
$config['total_rows'] = $this->_DB_FORUM->get('orderheader')->num_rows();
$config['per_page'] = 20;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config['uri_segment'] = 4;
$config['full_tag_open']='<div class="pagination"><div class="links">';
$config['full_tag_close']='</div></div>';
$this->pagination->initialize($config);
$this->_DB_FORUM->select('some query');
$query = $this->_DB_FORUM->get('table_name', $config['per_page'], $this->uri->segment(4));
I am returning result of query.
return $query->result_array();
Now, when I click on page numbers page gets reloaded and so all reset. I know there must be issue with $config['base_url'] = site_url().'/home';, so what should be here that i will be on same view, only sub-view should get reload and pagination should work.
I am not getting as I am beginner with CI. Thank you in advance.

how to create pagination for url with parameters in codeigniter?

we have an application which has page's result on the bases of certain parameters like domain.com/index.php/welcome/student/male here welcome is controller student is method name male is parameter, now we want pagination for this, but the problem is a person can also use sub category like this domain.com/index.php/welcome/student/male/steven where steven is another parameter, how to create pagination for this type of urls. Thank you in advance :)
Some problems when having optional parameters along with pagination:
The placement of pagination offset changes in the URL based on the
number of provided parameters.
If optional parameters are not set,
the pagination offset will act as a parameter.
Method 1:
Use query strings for pagination:
function student($gender, $category = NULL) {
$this->load->library('pagination');
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['base_url'] = base_url().'test/student/'.$gender;
// add the category if it's set
if (!is_null($category))
$config['base_url'] = $config['base_url'].'/'.$category;
// make segment based URL ready to add query strings
// pagination library does not care if a ? is available
$config['base_url'] = $config['base_url'].'/?';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
// requested page:
$offset = $this->input->get('offset');
//...
}
Method 2:
Assuming the category would never be a number and if the last segment is a numeric value then it's the pagination offset not a function's parameter:
function student($gender, $category = NULL) {
// if the 4th segment is a number assume it as pagination rather than a category
if (is_numeric($this->uri->segment(4)))
$category = NULL;
$this->load->library('pagination');
$config['base_url'] = base_url().'test/student/'.$gender;
$config['uri_segment'] = 4;
// add the category if it's set
if (!is_null($category)) {
$config['uri_segment'] = $config['uri_segment'] + 1;
$config['base_url'] = $config['base_url'].'/'.$category;
}
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
// requested page:
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 1;
//...
}
I prefer the first method because it does not interfere with function parameters and it would be easier to implement the pagination support at an abstract level.
I worked around with codeigniter but i think that maybe you have to use config/routes.php. For example with:
$route['^(bar1|bar2)/routes/(:any)/(:any)'] = "routes/get_bar/$2/$3";
you could write a function in controller (in this case routes) like this:
public function get_bar($bar1 = null, $bar2 = null)
I hope this help you
Regards!

CodeIgniter 404 error page not found in pagination

I have problem making CodeIgniter pagination when i click on link i get 404 page not found
my code in controler is
class Records extends CI_Controller
{
public function index()
{
$this->load->library('pagination');
$config['base_url']= 'localhost/records';
$config['total_rows']= $this->db->get('pages')->num_rows;
$config['per_page']= 2;
$config['num_links']= 5;
$this->pagination->initialize($config);
$data['query'] = $this->db->get('pages',$config['per_page'],$this->uri->segment(2));
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_list', $data);
$this->load->view('view_footer');
}
i tried diffrent uri segments, but still no result. ? I am writing code in right way. my base_url is empty in config file?
in view my code is
<?php
echo $this->session->flashdata('item');
echo '<h4>Lista podataka</h4>';
foreach ($query->result() as $q):
?>
<?php echo $q->info . br() . br()?>
<?php
endforeach;
echo $this->pagination->create_links();
?>
As far I understand you can't access your next page pagination? So I think it's because of this $config['base_url']= 'localhost/records';
you should put your complete url(including the controller name and function name), so it should be
$config['base_url']= 'localhost/records/index.php/controller_name/function';
and in your uri segment should $this->uri->segment(3)
try may be change uri segment
$this->load->library('pagination');
$config['base_url']= base_url().'/records/index';
$config['total_rows']= $this->db->get('pages')->num_rows;
$config['uri_segment'] = 3;
$config['per_page']= 2;
$config['num_links']= 5;
$this->pagination->initialize($config);
$data['query'] = $this->db->get('pages',$config['per_page'],$this->uri->segment(3));
For more follow :- pagination in codeigniter
Base Url needs to be
$config['base_url']= 'records/index';
Base url in must be contain the same function that is used to fetch data
it means $config['base_url']='localhost/project/records/index';

codeigniter pagination create_links()

The problem is that in pagination, i haven't got the database things showing up yet, but the problem isn't in that.
The problem is that I've got the page links showing up, but when I click the link to page 2, it goes to
http://127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/5.
As you can see, the link is basically repeated twice, now if I click 1 while on page 2, it takes me to
http://127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/
So as you can see, now it's written thrice, if I click 2 on this page it appends the url again and takes me there :/
Now I would like to ask WHY IS THIS happening???
Heres the code:_
Controller(pagination.php)
class Pagination extends CI_Controller {
function index() {
$this->load->library('pagination');
$config['base_url'] = '127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/';
$config['total_rows'] = $this->db->get('data')->num_rows;
$config['per_page'] = 5;
$config['num_links'] = 10;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('data', $config['per_page'], $config['uri_segment']);
$this->load->view('pagination_view', $data);
}
}
Here's the view(pagination_view.php):_
<html>
<head>
<title>CI Pagination</title>
</head>
<body>
<h1>Pagination With CI</h1>
<?php
echo $this->pagination->create_links();
?>
</body>
</html>
Just a bit of extra info, if I set $config['base_url'] to nothing, it links to
127.0.0.1:8887/5
Any help would be appreciated, is this a bug?
Try to use this:
$config['base_url'] = '/codeigniter-tests/index.php/pagination/index/';
I don't think that $config['base_url'] supports an incomplete URL. Try adding the protocol bit or leaving only the path:
$config['base_url'] = 'http://127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/';
$config['base_url'] = '/codeigniter-tests/index.php/pagination/index/';

how to pass arguments in codeigniter php controller functions

hi friends i have this function to show all articles, i am writing this function again and again for different categories, because codeigniter arguments are related to url how do i pass arguments so that i can reuse this function ?
This is my controller function to show all news.
function all_news(){
//do some pagination
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/news/all_news';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 7;
//some css for pagination
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
//initialize pagination
$this->pagination->initialize($config);
//end pagination
$data['title'] =" All News";
//for pagination
$data['query']= $this->db->order_by('id','desc');
$data['query'] = $this->db->get('articles',$config['per_page'],$this->uri->segment(3));
$this->load->vars($data);
$this->load->view('main/all_news');
}
Consider moving much of your controller logic into a model method. Then you'll be able send arguments to this method which will return back database results to your controller based on the arguments you send to the method.
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1-news
2-local
3-metro
4-crime_is_up
$product_id = $this->uri->segment(3);//metro
full info https://www.codeigniter.com/user_guide/libraries/uri.html

Categories