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'.
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.
I'm using CI Pagination library on my website and now with $config['use_page_numbers'] set to TRUE the current page is always the first. Everything works fine, except this.
Other settings:
$config['uri_segment'] = 2;
$config['prefix'] = "p";
$route['mypage/p(:num)'] = "citate/index/$1";
This is the function that calculate the current page (the output is correct). When I'm on first page returns 1, when on third page returns 3 and so on:
function getPagCurr($offset, $limit){
if( ! $offset) return $page = 1;
else return ($offset/$limit)+1;
}
... though, it's not working.
I've try to set up manually, just for testing, the value of $config['cur_page'] = 2 (so this means that the second link should be considered as active) but no change at all.
CI version is latest.
LE: SOLUTION
It seems that the prefix is the problem here. With my actual configuration the link will be like this www.site.com/mypage/p2, which is not working.
The working link would be www.site.com/mypage/p/2/ with the uri_segment = 3 and route mypage/p/(:num).
However, I really want to have the first link structure so here's my solution (not a good one because you have to modify some system library code):
Pagination.php (start line 166):
// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
$this->cur_page = $base_page;
}
..changed to:
// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
$current_page = $CI->uri->segment($this->uri_segment); //get pNUM
$current_page = substr($current_page, 1); //remove prefix
$this->cur_page = $current_page; //set current page
}
... and now it works!
If anybody have a better solution please tell!
Thanks.
Yes you are right it will not work because your segment got a p(p2)
To do this you must have to modify the core but i will say dont modify the core just extend the pagination class and modify the code with following:
Add a new class variable
var $use_rsegment = FALSE;
Then modify the create_links() around line 157
//add
$_uri_segment = 'segment';
if($this->use_rsegment){
$_uri_segment = 'rsegment';
}
//modify
if ($CI->uri->$_uri_segment($this->uri_segment) != $base_page)
{
$this->cur_page = $CI->uri->$_uri_segment($this->uri_segment);
// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
The uri rsegment is the new routed segment, now set the pagination config like this
$config['use_rsegment'] = TRUE;
$this->pagination->initialize($config);
So you can use both option when ever you need. When you have route set rsegment true
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
I am having trouble setting up pagination on codeigniter when I pass parameters in the URL
if my url is like this : search/?type=groups
what should be my $config['base_url'] for pagination to work?
if i set the base url to search/?type=groups the resulting url is search/?type=groups/10
which means $_GET['type']=groups/10
thank you
In pagination config:
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
Your current $_GET vars will be shown in pagination links.
You can replace $_GET by another associative array.
This won't add a query string unless one already exists.
Update:
I just saw, if you go back from another pagination number to click on the first(1), CI does not care anymore of your suffix config.
To fix that use $config['first_url'].
e.g: $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
The most up-to-date answer of this question is;
You should enable the reusage of the query string by enabling this configuration:
$config['reuse_query_string'] = true;
after that you should initialize the pagination:
$this->pagination->initialize($config);
Added $config['reuse_query_string'] to allow automatic repopulation
of query string arguments, combined with normal URI segments. - CodeIgniter 3.0.0 Change Log
Here is my jquery solution:
Just wrap pagination links in a div like this:
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
than add the following jquery code:
$("#pagination > a").each(function() {
var g = window.location.href.slice(window.location.href.indexOf('?'));
var href = $(this).attr('href');
$(this).attr('href', href+g);
});
Works fine for me.
if you are using codeigniter 2 there's an option in config.php, $config['allow_get_array'] - make sure its on TRUE.
Then set the pagination option $config['page_query_string'] to TRUE.
And finally, in your case set $config['base_url'] to "search/?type=groups", the pagination will append the per_page query string after it.
It should work this way, you'll get the offset in $this->input->get("per_page").
code strong!
I struggled with the same issue today. My solution is this:
Generate the pagination links and store them in a string ( $pagination = $this->pagination->create_links(); )
Use regexp to find all links and add query strings
The regular expression code used is:
<?php
$query = '?myvar=myvalue';
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
$unique = array();
if( preg_match_all("/$regexp/siU", $pagination, $matches) )
{
foreach ( $matches[2] as $link )
{
if ( !isset($unique[$link]) )
{
$data['pagination'] = str_replace($link . '"', $link . $query . '"', $data['pagination']);
$unique[$link] = '';
}
}
}
unset($unique);
Works like a charm for me! What it does is:
Find all links
Replace unique links (since there is a previous/next links same link may appear more than once) with the original link and the query-string.
Then just assign the variable to the template that will be shown and use print $your_pagination_variable_name; to show the links with your query-strings attached!
I think you are trying to do the same thing I was trying to do and I got it to work correctly by not setting a base url and just using this setup it kept me from having to manually editting the library
$this->load->library('pagination');
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Using the $config['suffix'] is the IMO best way to implement this because it doesn't require any extra processing as the regex solution. $config['suffix'] is used in the rendering of the urls in the create_links function that's part of the system/libraries/Pagination.php file so if you set the value it'll be used in the generation of the urls and won't require anymore processing which means it'll be faster.
Thanks for the post, this saved me tons of extra, not needed, coding!
Before line:
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
Replace this code below:
if(isset($_GET[$this->query_string_segment]))
{
unset($_GET[$this->query_string_segment]);
}
$uri = http_build_query($_GET);
$uri = empty($uri) ? '?' : $uri . '&';
$this->base_url = rtrim($this->base_url).$uri.$this->query_string_segment.'=';
Just see this link.
Just update the modified pagination class and then add
$config['get'] = "?string=" . $_REQUEST['string']."&searchBy=" . $_REQUEST['searchBy'];
The solution is that CodeIgniter does not function like that.
what I need is a method ( in the controller ) for each one of the options in "type"
so one option would be a method called :groups , another called entries etc etc
each method refers to a different model class or method as needed.
I am trying to better understand OOP and CI ...a bit of adjusting to do ... feel free to comment and correct me if i am wrong.
thank you
I got the same problem. My solution is to modify Pagination and put it in application/libraries.
First i create
var $get='';
and find all "a" elements and add $get in the href="........'.$this->get.'"'>.......</a>
Now in the controller or model input these line
$config['get']=(isset($_GET['search']))?'?search='.$_GET['search']:'';
That's it! i hope it will help you.
IN YOUR CONTROLLER:
$config['anchor_class'] = "class='link-pagination'";
IN YOUR VIEW:
$(".link-pagination").each(function() {
$(this).attr("href", $(this).attr('href') + "?id=<?= $this->input->get('id') ?>");
});
Had the same problem but realized that newer versions of CI have a suffix defined within the pagination class. Though it's still not in the documentation. No need to hack it.
I have encountered with similar kind of problem, and based on the above answer here is what I have to do for customization according to my needs.
My URI was to be something like this:
base_url() . /search/?term=
So, here is what I have done:
$config['base_url'] = base_url ."/search/?term=" . $_GET['term']
Hope you'll find the answer on this page:
LINK. Edit your answer back in if you can.