[SOLVED] Get product id knowing its name?

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
User avatar
noosphere
Forum Members
Forum Members
Posts: 54
Joined: Tue Feb 26, 2008 9:20 am

[SOLVED] Get product id knowing its name?

Post by noosphere »

Hello everybody...
just facing a new challenge...I can't solve :(
I'd like to get the ID of a product (CG Products module) but I know only the name.

Do you know how to do it ?
Thank you in advance !

Have a nice day ;-)
Last edited by noosphere on Fri Apr 26, 2013 6:53 pm, edited 1 time in total.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: How to: Get product id knowing its name?

Post by Dr.CSS »

Isn't it the first thing on the left next to the products name..?
User avatar
noosphere
Forum Members
Forum Members
Posts: 54
Joined: Tue Feb 26, 2008 9:20 am

Re: How to: Get product id knowing its name?

Post by noosphere »

Dr.CSS wrote:Isn't it the first thing on the left next to the products name..?
In the admin yes....it's that easy ! ;-)

I meant in my template ?
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: How to: Get product id knowing its name?

Post by Dr.CSS »

{entry->id} ..?
User avatar
noosphere
Forum Members
Forum Members
Posts: 54
Joined: Tue Feb 26, 2008 9:20 am

Re: How to: Get product id knowing its name?

Post by noosphere »

I'd like to get the ID of a product (CG Products module) knowing only the name.

Do you know how to do it ?
Thank you in advance !

Have a nice day ;-)
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: How to: Get product id knowing its name?

Post by Wishbone »

OK.. I'll bite.

Create a UDT called "get_product_id" containing:

Code: Select all

$name = $params['name'];
$assign = $params['assign'];
$product_id = '';

$db = cmsms()->GetDb();
$query = 'SELECT id FROM '.cms_db_prefix().'module_products WHERE product_name= ?';
$row = $db->GetRow($query,array($name));

if(!is_array($product)) {
  $product_id = $row['id'];
}

$smarty->assign($assign, $product_id);
Usage:

Code: Select all

{get_product_id name='Mr. Potato Head' assign='product_id'}

The ID is: {$product_id}
User avatar
noosphere
Forum Members
Forum Members
Posts: 54
Joined: Tue Feb 26, 2008 9:20 am

Re: How to: Get product id knowing its name?

Post by noosphere »

Oh Wishbone !
That's it , thank you so much ;-)

And it's simple indeed...I really have to go deeper into UDT and db queries !!!

SOLVED
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: [SOLVED] Get product id knowing its name?

Post by Wishbone »

Small typo. Change to:

if(is_array($row)) {
$product_id = $row['id'];
}

Note that I removed the ! from isarray
User avatar
noosphere
Forum Members
Forum Members
Posts: 54
Joined: Tue Feb 26, 2008 9:20 am

Re: [SOLVED] Get product id knowing its name?

Post by noosphere »

yes !
the procedure was correct...now the code is correct and working ;-)
thank you !!!
Post Reply

Return to “The Lounge”