CodeIgniter Pagination : Does not create first page's link - php

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.

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.

CodeIgniter: Is possible to have current page link by pagination library?

I have created pagination item by CodeIgniter pagination library with following code:
echo $this->pagination->create_links()
Everything is working well.
Now i want to load data by ajax and i have done already ajax part. But problem is to make clicked item as a current item and to arrange link for current item as it is no longer current.
Suppose i have a pagination as following:
[1] 2 [3] [4] [5] [6] [>] [Last >]
Now 2 is current item and 4 is clicked item.
I have checked CodeIgniter Pagination library, but it doesn't have any option to enable or disable current page link. Is it possible to have current page link without modifying the library?
Thanks in advance.
I take it all back. The Pagination library will need to be changed.
https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Pagination.php
Line 560
$output .= $this->cur_tag_open.$loop.$this->cur_tag_close
Will need to be replaced with
$append = $this->prefix.$i.$this->suffix;
$output .= $this->num_tag_open.'<a href="'.$this->base_url.$append.'"'.$attributes.$this->_attr_rel('start').'>'.$loop.'</a>'.$this->num_tag_close;
That should do it.
You don't need to modify anything on server side. just use plain old vanila jquery.
Here's how to do it.
Let's say i wrapped the links in a <div id="pagination"></div>
Now, to call ajax, use jQuery(body).on('click','#pagination a', function(e){....}).
Here in this code, jQuery(this) will refer to the link that was clicked.
Next, after successful call, remove the active (or any other class that is given ) from all the links. like jQuery('#pagination a').removeClass('active').
Next, if you still remember, jQuery(this) still refered to the current clicked item. (better save it as var current = jQuery(this) at the start of the clicked even so that the reference does not change. Now, just add the class. jQuery(current).addClass('active')
You are done! No need to do anything fancy anything in server side. Sometimes client side is enough.
P.S. i forgot how CI renders the pagination links. if you give the links, i can write the actual code.
var_dump($this->pagination); everything is inside
then if you check Pagination.php library you have these params available too:
var $cur_tag_open = '';
var $cur_tag_close = '';
so try calling them when initializing pagination
$pagination['cur_tag_open'] = '<span class="current-link">';
$pagination['cur_tag_close'] = '</span>';
$this->pagination->initialize($pagination); //now current link should be wrapped into the <span class="current-link"></span>
I don't believe you need to change anything in the Pagination library. You just need to use the uri_segment() function.
The Pagination library takes an argument as follows:
$config['uri_segment'] = 5;
The default value is 3. [Use a value that will work with your URI structure].
This variable is used along with the uri_segment() function to determine the current page, in the latest version of CodeIgniter 2.1.4 you will find it starting on line 142 in the Pagination.php library.
The appropriate uri_segment for the page identifier could be calculated as follows, if using a URI like this one: http://example.com/controller/function/page/20 then you can get the current page as follows,
$page = $this->uri_segment(4);
Hope that helps.
You really dont need to change anything.
Once you have your automatic page set
$page = 2 // Page should be set automatically
Now try this
$config["cur_page"] = $page;

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 url page number

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.

Categories