Change Codeigniter pagination query string - php

I want to change codeigniter pagination query string. Currently this is working like below example
http://example.com/1
I want to change this with my example like
http://example.com?page=1
Anyone can let me know how I can do this without changes in existing library? or Should I have to create my own pagination for this system?
Thanks

You have to set config for pagination
$config['page_query_string'] to TRUE
you also can configure your querystring
$config['query_string_segment'] = 'your_string';

Actually with CodeIgniter 3.0.0 there is a better solution now;
You should enable the reusage of the query string buy enabling this configuration:
$config['reuse_query_string'] = true;
only after that you should initialize the pagination:
$this->pagination->initialize($config);

You can do it by using 'Enabling Query Strings' in /application/config/config.php
$config['enable_query_strings'] = TRUE;
But, it will also add controller and method names as query stings not as clean urls like:
index.php?c=controller&m=method
Reference

Related

Query string creating problems for all methods in codeigniter

I am working on one project in that I am passing users data via query string in url , so I have enabled it inside config.php but after this if I try to call other methods in project then it is not working and again when disabled query string in config.php all methods are working fine, I don't know why enable query string affected on all methods in controller.
Note: I am using routing so it is lik this
$config['enable_query_strings'] = TRUE;// afte making true unable to call other methods in controller.
$route['xyz_method/(:any)'] = 'controller/method1/$1';
$link=urlencode(base64_encode("some_data"));
<li>Click here</li>
"...
CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you’ll see these items:
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
If you change “enable_query_strings” to TRUE this feature will become active. Your controllers and functions will then be accessible using the “trigger” words you’ve set to invoke your controllers and methods:
index.php?c=controller&m=method
Note
If you are using query strings you will have to build your own URLs, rather than utilizing the URL helpers (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with segment based URLs.
..."
See documentation here
To work this, try using this
$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings'] = TRUE;

Writing URL variables in CodeIgniter?

I'm working on a project using CodeIgniter, though I still a bit new on this PHP language framework.
I need to create multiple variables URL in CodeIgniter like:
index.php?page=10&filter=voted
How do you write it in CI? I know if it's only one variable then it should be
index.php/page/10
If the URL contains two or more variables, should it be like this or not? And how do you retrieve them since it uses URI Segment?
index.php/page/10/filter/voted
Much thanks for the help!
you can get data using segment.
$this->uri->segment(1);
$this->uri->segment(2);
$this->uri->segment(3);
or
get all segment in array
$segment = $this->uri->segment_array();
print_r($segment);

CodeIgniter Change URL Routes

I have a Real Estate Site, in CodeIgniter my link names is:
htp://examplesite(dot)com/property/31/en/property_title
I want to replace it with:
htp://examplesite(dot)com/my-custom-slug.htm
or
htp://examplesite(dot)com/property_title
Both ways might be ok. My routes are:
$route['default_controller'] = "frontend";
$route['404_override'] = 'frontend';
/* Custom routing */
$route['admin'] = 'admin/dashboard';
//$route['secret'] = 'admin/dashboard';
// To change property->listing in uri
$route['listing/(:num)/(:any)/(:any)'] = "property/$1/$2/$3";
$route['listing/(:num)/(:any)'] = "property/$1/$2";
what is the best way in this case?
I know there is a possible solution that:
Add
$config['listing_uri'] = 'listing';
Than add to:
In ‘application\config\routes.php’
After:
$route['listing/(:num)/(:any)/(:any)'] = "property/$1/$2/$3";
$route['listing/(:num)/(:any)'] = "property/$1/$2";
Add also:
$route['changed/(:num)/(:any)/(:any)'] = "property/$1/$2/$3";
$route['changed/(:num)/(:any)'] = "property/$1/$2";
but is there any way to remove the 'listing', to see only the clean url (for better seo purpose) ??
Welcome, luckily your problem isn't too big :)
Maybe have one slug before the property title to make the solution a bit more scalable:
$route["listing/(:any)"] = "property/view/$1";
Then point your browser to yoursite.com/listing/some_slug or yoursite.com/listing/12
You will need to have a slug associated with each property, otherwise you can use the id, and in the view function just retrieve the property based on the slug or id parameter.
If you don't want to include the listing/ you might have problems down the line when trying to create other routes.

Codeigniter Pagination class limitation

I am new to Codeigniter, and I have trouble with the pagination class.
From CI document: (I do studied it carefully and practiced it.)
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = '200';
$config['per_page'] = '20';
$this->pagination->initialize($config);
echo $this->pagination->create_links();
It works quite well, when I do practice at local host.
How can I pass more parameters for the base_url value?
Assumed I must pass mypara to page function as below.
$config['base_url'] = 'http://example.com/index.php/test/page/mypara/';
It doesn't work then. How can I joined my parameter (mypara) in the base_url?
It seems it doesn't support passing parameters to base_url.
Here's the answer on codeigniter forum. http://codeigniter.com/forums/viewthread/143280/P15T here are many questions that have been answered in there.
Here is an excellent tutorial from Nettuts on Pagination with COdeigniter
http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-7-pagination/
You might also change this
$config['uri_segment'] = '2';
to 3 or 4

CodeIgniter Routing

I am developing an ecommerce website with CI that has product categories and products. I want to route the URL so that it will go to the products controller, then run the getCategoryByName function for the first segment, then run the getProductByName for the second segment. Here is what I have:
URL:
products/docupen/rc805
routes.php:
$route['products/([a-z]+)'] = "products/getCategoryByName/$1";
$route['products/([a-z]+)/([a-z0-9]+)'] = "products/$1/getProductByName/$2";
But its not working. "docupen" is the category, and "rc805" is the product.
Thanks in advance.
Thank you all for your help. This is what I ended up with for what I needed.
$route['products/:any/:num'] = "products/getProductByID";
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";
My answer builds a bit on Colin's answer.
When I played around with the routes in CodeIgniter I came to the conclusion that the order of the routes was important. When it finds the first valid route it won't do the other routes in the list. If it doesn't find any valid routes then it will handle the default route.
My routes that I played around with for my particular project worked as follows:
$route['default_controller'] = "main";
$route['main/:any'] = "main";
$route['events/:any'] = "main/events";
$route['search/:any'] = "main/search";
$route['events'] = "main/events";
$route['search'] = "main/search";
$route[':any'] = "main";
If I entered "http://localhost/index.php/search/1234/4321" It would be routed to main/search and I can then use $this->uri->segment(2); to retrieve the 1234.
In your scenario I would try (order is very important):
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";
I don't know enough to route the way you wanted (products/$1/getProductByName/$2), but I'm not sure how you would create a controller to handle this particular form of URI. Using the $this->uri->segment(n); statements as mentioned by Colin in your controller, you should be able to do what you want.
You should use the URI class to retrieve the "docupen" and "rc805" segments from your url. You can then use those values in whatever functions you need.
For example, if your url is www.yoursite.com/products/docupen/rc805, you would use the following in your products controller to retrieve the segments and assign them to variables:
$category = $this->uri->segment(2); //docupen
$product = $this->uri->segment(3); //rc805
Then you can use $category and $product however you need to.
CodeIgniter routes don't work well with regex. They are supported, not I can never get them to work. It would be much easier to catch them like this
$route['products/(:any)'] = "products/getCategoryByName/$1";
$route['products/(:any)/(:any)'] = "products/$1/getProductByName/$2";

Categories