user2666633
user2666633

Reputation: 340

Change url in codeigniter with searches term

I have links like this

http://example.com/search/texas

I want it to route to a controller search and the URL should be

http://example.com/schools-in-texas

What I have done so far

On route

$route['search/(:any)'] = "search"; //search is a controller

On search controller I get the town name texas using

$x = $this->uri->segment(2);

I want to query the database and use the result to open a search template with the url being http://example.com/schools-in-texas where texas is the search term.

I redirect to the url but doesn't load the data from database so it's not working as expected.

$url = $this->uri->segment(2);
redirect('schools-in-'.$url, 'refresh');

Can someone provide any idea how to achieve this?

Upvotes: 0

Views: 282

Answers (1)

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

try this for routing

$route['schools-in-(:any)'] = 'search';

This will take into Search controller when your URL will be like http://example.com/schools-in-texas

And to get the word texas from URL (for search term) now you may use

$state_name = substr($this->uri->segment(1), strrpos($this->uri->segment(1), '-') + 1);

now $state_name will output as texas if you URL http://example.com/schools-in-texas

Upvotes: 2

Related Questions