user2666633 user2666633 - 1 year ago 71
PHP Question

Change url in codeigniter with searches term

I have links like this

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

What I have done so far

On route

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

On search controller I get the town name

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

I want to query the database and use the result to open a search template with the url being
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?

Answer Source

try this for routing

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

This will take into Search controller when your URL will be like

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download