Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
Perform simple calculations inside templates!
Lea Verou
Join Date: Jul 2005
Posts: 1,856

Greece
by Lea Verou Lea Verou is offline 23 Aug 2008

I was quite excited to find this out!

Have you ever wanted to make a simple calculation for a template, like an addition or subtraction? Personally, I needed this many times and most of them I created a plugin for that. Today I discovered that that is unecessary. You can use the conditional to make simple calculations. For instance, lets suppose you want to display somewhere the userid of the current user, incremented by one. You could do this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This will display as (given that the userid of the user viewing is 1):
The next userid is 2.
Of course the userid example is very simple, just to make you understand what I'm talking about.

Let me know if it helped you!
Views: 5407
Reply With Quote
Comments
  #2  
Old 30 Aug 2008, 01:19
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
interesting, thanks!
__________________
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team
Reply With Quote
  #3  
Old 24 Sep 2008, 01:39
Kaelon's Avatar
Kaelon Kaelon is offline
 
Join Date: Jan 2002
Very cool! Thanks, Michelle!
Reply With Quote
  #4  
Old 21 Oct 2008, 23:38
Chunky Monkey Chunky Monkey is offline
 
Join Date: Oct 2008
Is there a way we can use this to generate a random number and then test against it so that something happens only 1/X times?
Reply With Quote
  #5  
Old 21 Oct 2008, 23:41
Lea Verou Lea Verou is offline
 
Join Date: Jul 2005
Real name: Lea Verou
Nope. The functions for random number generation aren't allowed in templates, so you have to use a plugin.
Reply With Quote
  #6  
Old 22 Oct 2008, 00:33
Chunky Monkey Chunky Monkey is offline
 
Join Date: Oct 2008
Thank you, Michelle.
Reply With Quote
  #7  
Old 26 May 2009, 07:49
gmert gmert is offline
 
Join Date: May 2009
Real name: adr
Could I change the numbers by myself ????????
for examble, the number 41, to put the number 88 or number 14 ????







Last edited by gmert; 26 May 2009 at 08:17.
Reply With Quote
  #8  
Old 26 May 2009, 08:09
ragtek ragtek is offline
 
Join Date: Mar 2006
Location: austria, croatia
Originally Posted by Michelle View Post
Nope. The functions for random number generation aren't allowed in templates, so you have to use a plugin.
Mmaybe you could put it into the $safe_functions variable and use it this way
Reply With Quote
  #9  
Old 26 May 2009, 08:18
gmert gmert is offline
 
Join Date: May 2009
Real name: adr
How could i do it ??????
Reply With Quote
  #10  
Old 14 Jul 2009, 23:42
L2V L2V is offline
 
Join Date: May 2008
Anyone know what kinds of simple calculations can be done using this method?
Reply With Quote
  #11  
Old 15 Jul 2009, 00:01
Lea Verou Lea Verou is offline
 
Join Date: Jul 2005
Real name: Lea Verou
Originally Posted by L2V View Post
Anyone know what kinds of simple calculations can be done using this method?
Anything that doesn't involve functions or the functions involved are allowed within templates.
Reply With Quote
  #12  
Old 13 Aug 2009, 15:18
xlguy's Avatar
xlguy xlguy is offline
 
Join Date: Feb 2004
Thanks for sharing.

Could you do a multiplication? So

$newfigure = $oldfigure + $oldfigure * 0.15

Any idea?
__________________
xlguy
Reply With Quote
  #13  
Old 13 Aug 2009, 15:27
Lea Verou Lea Verou is offline
 
Join Date: Jul 2005
Real name: Lea Verou
Yes you can.
Reply With Quote
  #14  
Old 13 Aug 2009, 15:33
xlguy's Avatar
xlguy xlguy is offline
 
Join Date: Feb 2004
Thanks, sorry I should have been a bit clearer, what would the syntax be?
__________________
xlguy
Reply With Quote
  #15  
Old 13 Aug 2009, 17:58
Lea Verou Lea Verou is offline
 
Join Date: Jul 2005
Real name: Lea Verou

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

And after that, use $newfigure normally in your template
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 03:10.

Layout Options | Width: Wide Color: