I am facing the same problem and I searched google and found a question (answered & solved) at stackoverflow. But the solution did not solve my problem.
Can anyone tell me the issue.
The Stack Over Flow Question is : Codeigniter Pagination bug
Only the difference with the above question is I am not using Query String.
Following is my Controller Code:
// Pagination
$this->load->library('pagination');
$config['base_url'] = site_url('customer/index');
$config['total_rows'] = $total_records; // I have total 5 records
$config['per_page'] = $limit; // I have set the limit "2" for time being
$config['cur_page'] = $page;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
I am Getting the following result:
1 2 3 >
but on the "3" link I am getting Link to 4th page instead of 3rd.
Related
I'm building in CI3 and pulling data from a MSSQL database (no offset, are you kidding?), so i've been force to paginate from an array (see How to get CI pagination to work with a array();). Everything is working, I can manually add digits to the url (segment) and get the required results but i can't get the pagination links to show up at all.
I have been through the other similar questions on this site but none seem to relate.
Any Help would be greatly appreciated.
The Route - $route['orders/:num'] = 'orders/index';
The Model just pulls rows
Controller
public function index()
{
// Load Pagination Library
$this->load->library('pagination');
// Get results from MSSQL database
$pages = $this->orders_m->get_orders($this->session->customer_code);
// Initiate chunks array to create pages
$chunks = array();
// Offset used to index of chunk arrays to display relevent results, default to 1 if segment is missing.
$offset = $this->uri->segment(2,1);
// How many items to show per page
$limit = 10;
// an index you can use to find offsets for pagination
$index=0;
// count the retured results from the query
$count = count($pages);
// loop through the pages array in chunks, and create
// an index or offset you can query from
foreach(array_chunk($pages, $limit) as $page){
$index++;
$chunks[$index] = $page; // build your array
}
// add to a data array to be passed to the view
$data['orders'] = $chunks[$offset];
// Set config options for the pagination class
$config = array(
$config['base_url'] = base_url().'orders/',
$config['uri_segment'] = 2,
$config['total_rows'] = $count,
$config['per_page'] = $limit,
$config['display_pages'] = TRUE
);
// Initialise the pagination class
$this->pagination->initialize($config);
// Add the links to a data variable to be sent to the view
$data['links'] = $this->pagination->create_links(); // FAIL
// The 'why the fuck isn't it working' test procedure
print_r($data['links'])."<br>"; //FAIL;
print_r($config)."<br>"; //OK
echo $count."<br>"; //OK
echo $offset."<br>"; //OK
echo "<pre>";
print_r($data['orders']); //OK
echo "</pre>";
//$this->load->view('elements/header');
//$this->load->view('orders', $data);
//$this->load->view('elements/footer');
}
I think you have not added pagination view in your view file .Please check and add these lines:-
<div class="pagination" style="float:right; margin:20px 0px 20px 0px;">
<ul>
<?php echo $pagination_links; ?>
</ul>
</div>
Worked out the answer. As it isn't pulling rows from the database, instead it's using chunks from the array, per page needed to be one, as one chunk = 10 rows.
$config['base_url'] = base_url('orders');
$config['total_rows'] = ceil($count/$limit);
$config['per_page'] = 1;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 5;
Ended up ditching this method anyway and used datatables to manipulate the data and perform the pagination.
This is the code i currently have.
P.S. I am currently editing this, trying to solve this problem whilst waiting for someone to solve it here. Im not quite sure why this is happening.
Pagination:
$character = $this->uri->segment(3);
$config['base_url'] = base_url() . 'index.php/Controller/function/'.$character;
$config['total_rows'] = $this->Model->browse_total_rows();
$config['per_page'] = 1;
$config['num_links'] = 10;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
if(!is_numeric($character)){$data['records'] = $this->Model->get_records_filtered($character,$config['per_page'],$this->uri->segment(4));}
else{ $data['records'] = $this->Model->get_records($config['per_page'], $this->uri->segment(3));}
$data['links'] = $this->pagination->create_links();
$this->load->view('browse', $data);
Whenever i go to my browse page, instead of being on the page 1 of my page on pagination. it is defaulted that im on page 2. Why? because i see this as my link.
1 2 3 >
Plus, i can't click on link 2 but i can on link 1. I don't know if this is how pagination_links works. but i find it odd. Anyways, im not sure if its related to my other problem.
The character part is for the letter chosen, so for example i only want to view results with A as their first letter the url should be.
index.php/controller/function/A/
So the per page of the pagination will be placed on the 4th segment. It has no error that way. But when i don't click on any letter and just show all results.
Whenever i click on a link(per page). The url looks like this.
index.php/controller/function/2/
but when i click on the next page, it goes as
index.php/controller/function/2/4
so basically breaks the pagination and retrieves no result.
I have tried other solutions like adding IF/else statement that if character is null or not equal to range('A','Z') then the $config['base_url'] should have no .$character on the end and the uri_segment should be on 3rd. But it still goes the same as above.
EDIT:
The 2nd problem was solved, i used something like this.
$character = $this->uri->segment(3);
$Alphabet = range('A','Z');
$flag = 0;
for($i=0;$i<26;$i++)
{
if($character==$Alphabet[$i])
{
$flag +=1;
}
}
Seems like, if($character==range('A','Z')) doesnt work as only now noticed when i echoed that range returns results of array so i had to loop. is there any better way to check than what ive done above?
With your pagination config, you have the pagination segment set to 4 regardless of the whether a letter is specified or not. But it should be 3 when there is no character if I'm understanding correctly.
Try something like this:
if(!is_numeric($character)) {
$config['uri_segment'] = 4;
$data['records'] = $this->Model->get_records_filtered($character,$config['per_page'],$this->uri->segment(4));
} else {
$config['uri_segment'] = 3;
$data['records'] = $this->Model->get_records($config['per_page'], $this->uri->segment(3));
}
$this->pagination->initialize($config);
For checking for a character in the range, you can use
if (in_array(strtoupper($character), range('A', 'Z'))
The strtoupper() is optional. Leave it in if you want 'a' to match 'A'.
Hi everyone can someone explain what dose this do
$this->uri->segment());
its part of my pagination function and I cant figure out why I need it and if there is something to replace it with. Its causing an error in my script. Thanx for ur help. Also im new to codeigniter and php plys try to make it simple form me tnx in advance.
error message
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/survay_view.php
Line Number: 31
here is my all code for my pagination function
//pagination
function page()
{
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/admin/index.php/survay/';
$config['total_rows'] = $this->db->get('save_survay')->num_rows();
$config['per_page'] = 1;
$config['num_links'] =10;
$this->pagination->initialize($config);
//print_r($this->uri->segment());die;
$data['records'] = $this->db->get('save_survay', $config['per_page'], $this->uri->segment());
$data['pagination'] = $this->pagination->create_links();
$this->load->view('survay_view', $data);
}
}
The variable $this->uri->segment(); means a particular URI segment.
So for example if you have a URL /page/view/1
If you did echo $this->uri->segment(2); it would return 'view'.
See http://ellislab.com/codeigniter/user-guide/libraries/uri.html for further information.
this $this->uri->segment(); is actually pick up the value from url. just like $_GET[].
see http://ellislab.com/codeigniter/user-guide/libraries/uri.html for details help.
I need to change the href of <a> tag in CodeIgniter pagination.
CodeIgniter's $this->pagination->create_links(); function creates links like this:
3
But, I need the page number(3) at the end of all segments.
Like this:
3
3
How can I do this?
You need to pass in the URL as config variable:
$config = array('base_url' => site_url('/admin/view/field/created'));
$this->pagination->initialize($config);
$this->pagination->create_links();
For more config possibilities, see documentation.
I ran into this too. Easiest solution? Reconfigure your URL structure so the pagination is always the first segment. To not mess up the sorting fields just make sure the page always loads with a pagination, ie when it first loads put a 0 in that segment. Other than changing your set up to use query strings I don't think you can move where the pagination segment is easily, I suppose you could do some if logic in your config such as:
if(is_numeric($this->uri->segment(2)
{
$config['uri_segment'] = 2;
} else if (is_numeric($this->uri->segment(3) {
$config['uri_segment'] = 3;
}
But honestly that could get pretty ugly depending on how many extra segments you're adding.
You can try to calculate the uri segment of the "page number" and the "base url" like this:
// Parsing the URI into an associative array
$uri = $this->uri->uri_to_assoc(4);
$segments = count($uri);
// Calculate the uri segment and base url
if ($segments > 1) {
$uri_segment = 3 + $segments - 1; // 3 segments "index.php/admin/view/ + SORT_SEGMENTS - PAGE_SEGMENT
array_pop($uri); // Pop the page number
$base_url = site_url('admin/view/'. implode('/', $uri));
} else {
$uri_segment = 4;
$base_url = site_url('admin/view/');
}
// Pagination config
$config['base_url'] = $base_url;
$config['uri_segment'] = $uri_segment;
$config['use_page_numbers'] = TRUE;
$config['total_rows'] = YOUR_CONFIG;
$config['num_links'] = YOUR_CONFIG;
$config['per_page'] = YOUR_CONFIG;
$this->pagination->initialize($config);
$pagination = $this->pagination->create_links();
PD: I'm argentinian and my English skills are a little poor
Let me explain my issue in detail
I have a table called products
Products : Name , views , sale_wanted , added_date
I am listing all my products in a view .
I am handling sale wanted through a flag means 0 or 1.
There are some links to sort the listing.
Most Viewed
Wanted
For Sale
New Arrivals
Now when user clicks all the listing is sorted according to the parameter i am sendig.
I want to use pagination class of Codeigniter.
Here comes some issues.
When i am clicking let suppose Wanted and sending a parameter it lists all the products wanted.
Now i click on a pagination link and the wanted parameter is gone
and the list becomes without wanted parameter.
Same goes with the other Anchors.
I have to restrict it so it still has my parameter.
The second problem is that what i see Codeigniter is laking.
I need some links that user can select. Means i want to give the user functionality to select how many products he wants to see in one page.
Let suppose 5 ,10 ,15 ,20
Choosing the greater number will reduce the number of pages.
And still i want the same functionality.
The important point is that i want to handle all this just in one shot means i dont want to duplicate my code for every anchor.
I need suggestions from expert and any help regarding Pagination library.
$seg=3
$noOfresultsToshow=$this->uri->segment('5')//use if else for by default no of results
$typeOfresult=$this->uri->segment('4'); //Most Viewed Wanted For Sale New Arrivals
$config['total_rows'] = $countqueryresults;// mention the total rows here
$config['per_page'] = $noOfresultsToshow;
$config['uri_segment'] = $seg;
$this->pagination->initialize($config);
$data['yourvariable'] = $this->model_name->getData($config['per_page'], $this->uri->segment($seg),$typeOfresult);
$data['links'] = $this->pagination->create_links();
in your view file echo $links;
I have found a solution for the issue.
I have made two methods in Codeigniter Pagination library
Here they are
Defined two new variables
var $base_link = '';
var $per_page_link = TRUE;
var $per_page_array = array();
The user can define both of them while initializing
$config['per_page_link'] = TRUE;
$config['per_page_array'] = array(5,10,15,20);
$this->pagination->initialize($config);
Now the methods
public function create_per_page()
{
$CI =& get_instance();
$output = '';
$current = $CI->uri->segment(3,5);
if($this->per_page_link === TRUE AND count($this->per_page_array) > 1){
foreach($this->per_page_array as $row){
if($current == $row){
$output .= $this->cur_tag_open . $row . $this->cur_tag_close;
$output .= ' ';
}else{
$output .= ''.$row.'';
$output .= ' ';
}
}
}
return $output;
}
And the other is create_per_page_links(). I have just copied the code of create_links method in it and modified it. In the start of create_per_page_links() i have added these extra lines.
$this->per_page = $CI->uri->segment(3,5);
$this->uri_segment = $CI->uri->segment(4,1);
However, This will only work with segments on. i haven't tested it with query string. Also if per_page_link is FALSE both methods will not work.
Calling create_per_page_links will generate the following
« First < 1 2 3 4 5 > Last »
And create_per_page will generate the following
5 10 15 20
And Done