Showing product name instead of product id in the URL. |
Hi guys.
i am new to CodeIgniter. I have a controller named product. and i am passing parameters to product controller index function using routes. $route['mobiles/(:num)'] = 'Product/index/$1'; so it looks like this ( http://localhost/igniter/mobiles/12 ) But my problem is i want to show the name of the product in the url not the id. i want this ( http://localhost/igniter/mobiles/Honor-10) or ( http://localhost/igniter/Honor-10) Any help will be appreciated. Thanks (07-02-2018, 10:04 PM)mianaliasjad Wrote: Hi guys. $route['mobiles/(:any)-(:num)'] = 'Product/index/$2'; The URL should be: http://localhost/igniter/mobiles/Honor-10 The parameter you receive in index method is 10 (second parameter $2)
You are half way there.
There's little config change you need to do, changing (:num) to (:any) PHP Code: $route['mobiles/(:any)'] = 'Product/index/$1'; Then you need to handle the incoming attribute on controller side to get ID out of 'Honor-10'.
(07-03-2018, 12:31 AM)Pertti Wrote: You are half way there. Then according to this, i have to make product name primary key in my table of database. can't i just say that i search from the database with integer id but show name in the url?
(07-02-2018, 11:56 PM)huangnam Wrote:(07-02-2018, 10:04 PM)mianaliasjad Wrote: Hi guys. i think you are thinking in Honor-10 the 10 is id. no honor-10 complete is mobile name not id. what i want is to show name in url but in method also get numeric id so that i search with that in databse. (07-03-2018, 12:40 AM)mianaliasjad Wrote: Then according to this, i have to make product name primary key in my table of database. You can change your database query. Assuming you select it by ID right now. PHP Code: $product = $this->db You then specify what field you want to select by PHP Code: $product = $this->db You don't have to change your DB primary key, it still can be ID, but you should index the name field. |
Welcome Guest, Not a member yet? Register Sign In |