My current URL is like:
www.hostname.com/get_city/
(this url is already shorten by using routes)
Here get_city is my method name and now what i exactly want is...
1)remove controller name from url
2)pass selected city value from dropdown and set in URL as a parameter
So,Required url is: www.hostname.com/california OR www.hostname.com/newjersey
NOTE:I know how to use routes but in such case how to make dynamic URL?!
AND please don't give me direct reference of ellislab docs because i have already tried those things
For dynamic route in codeigniter:
try like this:
in your routes.php file copy and paste these codes:
require_once (BASEPATH . 'database/DB' . EXT);
require_once (BASEPATH . 'helpers/url_helper' . EXT);
require_once (BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();
$query = $db -> get('news');
$result = $query -> result(); //result return all records
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> subject)));
$route[$string] = "controller/news_details/$row->id";
}
so you can , change $string with any string that you want.
then try type new url and see Routes will work fine.
NOTE:.htaccess file must be removing index.php in url
hope this help.
You can try with the method "_remap".
Check the official documentation for more info about the behavior of the function:
http://www.codeigniter.com/user_guide/general/controllers.html
Regards.
You need to config routes in application/config/routes.php
After existing routes add
$route['([a-z]+)'] = 'controller_name/method_name/$1';
But this overwrite all routes and before this route you need to declare all routes for you controllers
$route['product/:any'] = 'product/$1';
$route['catalog/:any'] = 'catalog/$1';
and after
// this route be used when previous routes is not suitables
$route['([a-z]+)'] = 'controller_name/method_name/$1';
DOCS:
http://code-igniter.ru/user_guide/general/routing.html
CI2: URI Routing
CI3: URI Routing
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
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.