[thelist] PHP / smarty

Phil Turmel philip at turmel.org
Sun Oct 9 12:04:36 CDT 2011

Hi Bob,

I'm a bit rusty with Smarty, but I think I can help:

On 10/09/2011 12:30 PM, Bob Meetin wrote:
> I am working a project, a shopping cart, which is done using smarty
> which I don't know.  I am making some customizations to the product
> page which require running a query against the product ID.  Looking
> at the smarty template files, the product ID can be displayed using
> this smarty code:
> {$product.product_id}
> as in: <h4>Product ID: {product.product_id}</h4>
> The question. How can you convert the smarty ID to a PHP variable
> that can be used in the PHP/MySQL query?  Example:
> <?php
> $product_id = some PHP function on product.product_id ; // meaning
> what php code do I use to extract the ID out of smarty
> $query = "select pcid, pid, group, title, description from
> product_xdesc where pid = $product_id";
> ?>
> I will probably set up the PHP as an include file.  Once I have the
> $product_id passed to PHP, the rest is easy.

I think you'll find what you need much quicker if you find the call to the Smarty template in the existing PHP code for that page.  (Smarty templates are called from PHP.)

You should find a line containing: $smarty->assign('product', ....) or $smarty->assign_by_ref('product', ....) where the .... is a PHP array.  That array will have the key 'product_id' that you are looking for.

Add your new PHP code to that section, before the $smarty->display(....) or $smarty->theme_display(....), and put your SQL results array into another 'assign' call.  Then they'll be available in the template in the same way as the 'product' array.  You may even be able to add your code before the $smarty->assign('product', ....) and include the new items in the same 'product' array.

If you share some more of the existing PHP I can probably be more specific.



More information about the thelist mailing list