Adding PHP Variables Listed In Smarty Template Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Adding PHP Variables Listed In Smarty Template Page

Post by jbh »

Hey,

I'm trying to get 2 values that I have listed in a smarty template from Amember membership software to add up.

For instance, in the smarty template page, this line of code:

{$d.sales|string_format:"%d"}

= # of sales. This comes from a db query I create. Ok, so to the user let's say that value is '5' when they look at the page

And the next line is:

{$d.uniq|string_format:"%d"}

= # of unique VIEWS.

All I want to do is a legal math function (division) within the smarty template, such as

$div_ratio = {$d.sales|string_format:"%d"} / {$d.uniq|string_format:"%d"}
print $div_ratio;

So, within the smarty template would be a variable that divides these 2 lines of code to create a dividend for the user to view.
But when I try to turn them into variables within the smarty template, it breaks a million times.

Is it POSSIBLE to add/subtract/divide values WITHIN the smarty template html page or must EVERYTHING be done within the php pages that display them?

Thanks
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: Adding PHP Variables Listed In Smarty Template Page

Post by lonelywolf »

Maybe there are some solutions for your problem.
-First(not good, it's a little complex), you can print smarty variables in seperate div tags(using id attribute to identificate). After that, you use javascript to grab those variables in div tags(maybe using document.getElement...), parse to correct types, calculating and printing results in some predefined div tags.

-Second, use {php}{/php} block in smarty - not recommend.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Adding PHP Variables Listed In Smarty Template Page

Post by WebbieDave »

Absolutely it's possible to do math within smarty templates. Make sure you're dividing numbers. Try:

{$d.sales/$d.uniq|string_format:"%d"}

Check out the smarty docs for more info.
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Re: Adding PHP Variables Listed In Smarty Template Page

Post by jbh »

Ok, I'll go there for the official tutorials. It's things like math functions and declaring vars or re-formatting strings that
seem to confuse me.

Thanks
Post Reply