CodeIgniter Forums
Showing product name instead of product id in the URL. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Showing product name instead of product id in the URL. (/showthread.php?tid=71074)



Showing product name instead of product id in the URL. - mianaliasjad - 07-02-2018

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


RE: Showing product name instead of product id in the URL. - huangnam - 07-02-2018

(07-02-2018, 10:04 PM)mianaliasjad Wrote: 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


$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)


RE: Showing product name instead of product id in the URL. - Pertti - 07-03-2018

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'.


RE: Showing product name instead of product id in the URL. - mianaliasjad - 07-03-2018

(07-03-2018, 12:31 AM)Pertti Wrote: 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'.

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?


RE: Showing product name instead of product id in the URL. - mianaliasjad - 07-03-2018

(07-02-2018, 11:56 PM)huangnam Wrote:
(07-02-2018, 10:04 PM)mianaliasjad Wrote: 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


$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)

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.


RE: Showing product name instead of product id in the URL. - Pertti - 07-04-2018

(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.
can't i just say that i search from the database with integer id but show name in the url?

You can change your database query.

Assuming you select it by ID right now.

PHP Code:
$product $this->db
    
->where('id'1)
    ->
limit(1)
    ->
get('product'); 

You then specify what field you want to select by

PHP Code:
$product $this->db
    
->where('name''Honor-10')
    ->
limit(1)
    ->
get('product'); 

You don't have to change your DB primary key, it still can be ID, but you should index the name field.