I am facing an issue with my site on cars - Now the problem i am facing with CodeIgniter is that on "new car" section if I select make, the URL is like below.
http://carandme.com/new-cars/ashok-leyland-cars-in-india?car_type=new&car_type_id=1&maker_id=106&maker_identifier=ashok_leyland&model_id=&model_identifier=
but I want a URL like this.
http://carandme.com/new-cars/ashok-leyland-cars-in-india
Similarly I am also facing issue in refine search on same page i.e if I select two car, the URL doesn't changes but the parameters do. Is there any way for this to be without parameter.
$input1 = $this->input->post('user_search1');
$input2 = $this->input->post('user_search2');
$user_input = array($input1, $input2); //making array of user search Inputs
$search_field_name = array("search_col_name1", "search_col_name2"); //original column name of table fro searching
//combining to send it as uri latter
$searchparams_uri = array_combine($search_field_name, $user_input);
//removing the keys and values from the array if Values is null or not set
$searchparams_uri = array_filter($searchparams_uri);
// Convert to associative array $this->terms_uri to segments to append to base_url
$keys = $this->uri->assoc_to_uri($this->terms);
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/controller/method_name/'.'/'.$keys.'/';
You will get URL like this:
index.php/controller/method_name/search_col_name1/input1/search_col_name1/input1
Break and Make according to your need. Question are welcome.
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'.
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
I'm working an a shopping cart with a page called display.php. This page is called repeatedly as you go through the navigation, always calling itself but querying for new, more specific things. I am trying to make breadcrumbs that will provide links to previous pages, but it's proving to be tricky.
This part of display.php retrieves "id", which the query uses to find things, and breadcrumbs, which is the previous breadcrumbs string. "id" is always added to the breadcrumbs string.
$id = $_GET['id'];
$breadcrumbs=$_GET['breadcrumbs'];
$breadcrumbs = $breadcrumbs." > ".$id;
echo $breadcrumbs
As the id variable is used to query for things, links are generated to go to display.php all over again but to query for something new and take the breadcrumbs string with it.
<img src="imagen/logo1.jpg" alt="" width="100" height="100" /><br>'. $name . '
Each time I go to a new page, the breadcrumbs display visibly, but how can I safely change these string statements into links? an can't carry another string inside it safely. I think it's possible if I explode the breadcrumb string with the > character as the delimiter. How can I fix this up so it will display nice links in the breadcrumb string?
$breadlist = explode(" > ", $breadcrumbs);
$linkarray = array();
foreach $breadlist as $breadlink
{
$linkarray($j) = "<a href=display.php?id=".$breadlink($j)."?breadcrumbs=".$breadcrumbs.">".$breadlist($j)."";
};
Two apparent problems (among some other small fixes):
You need to urlencode() your URLs.
You need to access arrays with [], not ().
Here is an example to get you started:
$breadlist = explode(" > ", $breadcrumbs);
$linkarray = array();
foreach( $breadlist as $breadlink)
{
$url = 'display.php?id=' . $breadlink . '&breadcrumbs='.$breadcrumbs;
$linkarray[] = '' . $breadlink . '';
}
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.