CodeIgniter pagination url page number - php

From what I saw, CodeIgniter's pagination is counting the page wrong way, because I got pagination looking like this:
1 2 3 >
And its good, the problem is in the each pagination number url, except the first one:
Number 2 from the pagination has the following url:
http://my-url.com/index.php/page/1
And number 3 has the following url:
http://my-url.com/index.php/page/2
So, the number is URL is decreased by 1 everytime.
How can I solve that, so the page numbers in urls will be the same as the page numbers in the pagination?
My pagination config:
$config['per_page'] = 5;
$config['base_url'] = site_url('page');
$config['uri_segment'] = 2;
$page = $this->uri->segment(2);
$total_rows_array = $this->records->get($config['per_page'], $page * $config['per_page']); // parameters: limit, offset.
$config['total_rows'] = count($total_rows_array);

Had the same problem you have, and I added this configuration line:
$config['use_page_numbers'] = TRUE;
and it works like a charm!

Are you using page numbers in the pagination config?
The way CodeIgniters pagination works (by default) is that you set the number of records to show in the code, CodeIgniter works out the offset and appends that to the URL.
The reason page 1 (if you will) has no number at the end is because there is no offset, page 2 will have an offset of 1 because it is offsetting that many records. Wow this is a lot harder to explain than I thought before I started typing this answer!
:EDIT:
Also, if you are using page numbers in the pagination config, I imagine that code igniter is taking the per_page amount and multiplying it by the page number to get the real offset for the records.
Page 2 would really work out as per_page (15) * page_number (1) = 15 for the real offset.

Related

Only load next section if previous section has finished paginating in Laravel (Multi Level Paging)

The Issue
Consider the following layout:
Now imagine that each section within the above list, is retrieved by an entirely separate query on the database...
The content on the page is paginated as the user scrolls (lazy loading). Resultantly, each section is only visible once the previous section has been completely loaded like so:
For the purposes of the following example, please assume the following:
Page size is 5
Section 1 has 6 items
Section 2 has 10 items
GET api.example.com/some_request?page=1
{
data: {
'section_1': [
// The first 5 items of section 1 are returned
]
},
laravel_pagination_stuff_etc...
}
GET api.example.com/some_request?page=2
{
data: {
'section_1': [
// The last item of section 1 is returned
],
'section_2': [
// The first 4 items of section 2 are returned
]
},
laravel_pagination_stuff_etc...
}
What I have tried
I have played around with Laravel Pagination like so:
// Determine which page to retrieve
$page = Input::get('page', 1);
$paginate = 5;
// Paginate the results
$offset = ($page * $paginate) - $paginate;
$items_for_current_page = array_slice($results, $offset, $paginate, true);
$results = new \Illuminate\Pagination\LengthAwarePaginator($items_for_current_page, count($results), $paginate, $page);
However, I am not familiar enough with the framework and library so before I spend hours/days going through the Laravel Pagination code, are there any Laravel pro's out there that can see a way to achieve the above in a simple way?
Additional Information
Just to be clear as to what I am asking; I am looking for a possible way to group and paginate two entirely separate queries into one page parameter. Is this possible?
Please note I am not asking, nor do I need, the code written for me. I am merely asking for a bit of guidance in the right direction in regards to the logic and the code.

How can i change the URL during pagination codeigniter

In My Controller:
public function testpages()
{
$config['base_url'] = "http://www.domain.com/index.php/testpages";
$config['total_rows'] = COUNT($data['names']);
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$this->load->view('nameshow',$data);
}
I want to show the names one by one,but when i click on number links generated by the pagination it shows the url like this:
http://http://www.domain.com/index.php/testpages/1
http://http://www.domain.com/index.php/testpages/2
I just want to change the uri segment as:
http://http://www.domain.com/index.php/testpages/firstnamevalue
http://http://www.domain.com/index.php/testpages/secondnamevalue
How can i show the url according to my need. with using of pagination, is there any way exist's in codeigniter. if yes then how.?
You can't change this functionality of the CI pagination plugin, the integer values are page results, or more accurately they are the offset.
Your SQL query consists of limit and offset.
The pagination will show say 10 records (on pg 1), and the link to page #2 would be:
http://http://www.domain.com/index.php/testpages/10
Why? Because your limit is 10 records per page (page 1), so page two is another 10 records and offset is 10 and records start on 11->20 (10), and page #3 will have a link of:
http://http://www.domain.com/index.php/testpages/30
Since you are paginating through number of results, you cannot do "starts with name" and "ends with name", that would have to be custom, and be some sort of range search and not simple limit and offset on result set.

Joomla Infinite Scrolling mysql pagination issues

I have set up infinite scrolling on a Joomla based website to load db results from mysql query. It works fine but when I have it set up to load 10 results at a time, it skips results 11-20 and then loads the rest of the values, and likewise when I set up to show 20 results it loads the first 40 without any repeats, and then proceeds to load 10 previouss results and 10 new ones for each new pagination result until it reaches the end of the list. Here is the code I have for pagination,
//
jimport('joomla.html.pagination');
// prepare the pagination values
$total = $this->xyz->getTotal('posts',' and cat_id = ' . $cat->cat_id);
$limit = $mainframe->getUserStateFromRequest('global.list.limit','limit', $mainframe->getCfg('list_limit'));
$limitstart = $mainframe->getUserStateFromRequest(JRequest::getVar('option').'limitstart','limitstart', 0);
$this->items = $this->xyz->categoryItems(JRequest::getInt('cat_id'),$limitstart,$limit);
// create the pagination object
$_pagination = new JPagination($total, $limitstart,$limit);
$_pagination_footer = $_pagination->getListFooter();
//
I should mention that I set the $limit value to 10 on line 7 of the code above to make it load 10 at a time. If it is left as $limit it loads 20 at a time.
Preferably I would like to load 50 at a time without any repeats or omissions but as it is now, I get plenty of repeats when set to 50. I found that setting it to 10 gives me the best results but still skips 11-20.
Any suggestions or thoughts would be greatly appreciated.
Had similar problems on two different occasions
1) SEF turned off
You might want to debug global.list.limit to check for consistency in values it loads
2) SEF turned on
Look for inconsistent entries in the redirection base for the same sef url.

CodeIgniter Pagination - first page’s link doesn’t work, why?

I use pagination library on my site, my config is here:
$data['url_keyword'] = url_title($keyword, '_');
$config['base_url'] = base_url().'image/'.$data['url_keyword'].'/';
$config['per_page'] = 1;
$config['uri_segment']=3;
my urls are: mysite/image/keyword/1 (page2), mysite/image/keyword/2 (page3), mysite/image/keyword/3 (page4), mysite/image/keyword/4 (page5)
When I’m on page 2 (mysite/image/keyword/1) and click previous - it gets me into mysite/image/keyword/ - which doesnt work, but mysite/image/keyword/0 WORKS. Library just doesn’t add zero at the end of url.
how to fix it?
Try this:
if (!$this->uri->segment(3))
$page = 0;
//Your other logic
By default if $this->uri->segment(x) is empty it will return false.
Without seeing your controller or view I can't be more specific on where to place this or what you are naming your variable. This should work in the instances in which you call: yoursite.com/image/keyword

CodeIgniter Pagination : Does not create first page's link

my pagination problem still continue ;
I just making pagination in simple stuff :
$config['base_url'] = site_url('admin/index/page/');
$this->load->database();
$config['total_rows'] = $this->db->count_all('sms');
$config['per_page'] = 20;
$offset = $this->uri->segment(4, 0);
$this->pagination->initialize($config)
And my view page has this command for view pagination :
<?php echo $this->pagination->create_links(); ?>
And after more than 20 rows , pagination starts to paging the list, but html output shows like this :
1 2 3
As in view , First Page Number 1 does not have link either jumping next page , there is no link on page number 1. Just its on strong .
My second problem is : I have just 30 record but CI pagination creates 3rd page which is coming with empty rows !
I am not sure why some Class ( specially pagination makes so much trouble to users ? ) If i need to pay something ( maybe hidden licence? ) for get away from trouble instead of using simple pagination class without loosing so much time for searching issue on internet , I am ready for it !
I was very mad because of this pagination problem and I was studying the source code of the pagination library and I saw this -- var $uri_segment = 3; The default of the pagination library uses the 3rd uri segment, in your case and in my case we wanted to use the 4th uri segment. To suit our needs change this code:
$config['base_url'] = site_url('admin/index/page/');
$this->load->database();
$config['total_rows'] = $this->db->count_all('sms');
$config['per_page'] = 20;
$offset = $this->uri->segment(4, 0);
$this->pagination->initialize($config)
TO
$config['base_url'] = site_url('admin/index/page/');
$this->load->database();
$config['total_rows'] = $this->db->count_all('sms');
$config['per_page'] = 20;
$offset = $this->uri->segment(4, 0);
$config['uri_segment'] = 4; // add this line to override the default
$this->pagination->initialize($config)
Kindly post back here if this will solve your problem or if the problem still exists so I can help :)
Nwei this is not in the pagination class documentation. I hope this will be added to the docs because I'm seeing many developers having a hard time with the pagination class.
If you are familiar with how pagination works, then you should be aware that pagination does not add a link tag to the current page. So I'm not sure what you're asking in regards to "pagination class does not create 1 page link".
Also, I don't see where you're offset is being used within the pagination functionality. It seems like you set it but you don't use it. This can cause an incorrect pagination result which I assume is why you get more pagination links than you expect.
$offset = $this->uri->segment(4, 0);
$this->pagination->initialize($config);
One way to change the markup for the "current page" link is 'Customizing the "Current Page" Link'
$config['cur_tag_open'] = '<a href="#">';
The opening tag for the "current" link.
$config['cur_tag_close'] = '</a>';
The closing tag for the "current" link.
The other option is to dive into the pagination class and remove the functionality it has for not adding a link to the current page.
Really, if you're on the current page, it doesn't add any benefit to link to the same page you're already on which I'm assuming is the reason the current page doesn't have a link. If you went to page 2 then the 2 would be disabled. Hope that makes some sense.
No, there no need to pay anything ...CI pagination works perfectly ..you see this library http://codeigniter.com/user_guide/libraries/pagination.html
make sure your $config['total_rows'] = $this->db->count_all('sms'); this code is working correct and providing the correct number of rows and also i would like to suggest use ..$config['uri_segment'] = 3; or $config['uri_segment'] = 4; depend on your urirather than using $offset = $this->uri->segment(4, 0);
Hofefully these few change will work for you....
I was also having the same issue in pagination when using segment number 4. I solved it by using segment number 3. Thats also my problem because I really need to use segment number 4 But Oh well it has bug. I hope this can be solved ASAP. Or if u really need to fix this issue now u can try to fork the source code of pagination class.

Categories