Welcome Guest, Not a member yet? Register   Sign In
Showing product name instead of product id in the URL.
#1

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
Reply
#2

(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)
Reply
#3

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'.
Reply
#4

(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?
Reply
#5

(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.
Reply
#6

(This post was last modified: 07-04-2018, 12:23 AM by Pertti.)

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB