We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Mathematical operations

Development and Programming

Yuri's avatar
Yuri
273 posts
16 years ago
Yuri's avatar Yuri

I wrote a small plugin that enables the usage of basic mathematical (arithmetic) operations (e.g. + - * / )

It can be useful especially in conjunction with conditional ({if}) statements.

The plugin takes 2 parameter, num1 and num2, which are the numbers you want to do something with

Call

{exp:math:add num1="10" num2="2"}

to add 2 and 10 (10+2)

{exp:math:subtract num1="10" num2="2"}

to subtract 2 from 10 (10-2)

{exp:math:multiply num1="10" num2="2"}

to multiply 10 and 2 (10*2)

{exp:math:divide num1="10" num2="2"}

to divide 10 by 2 (10/2)

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Hi yurrua,

Looks like a nice neat addition to the plugins for ExpressionEngine, well done!

When you say it can be used in conjunction with conditional {if} statements do you have any examples of this perhaps?

Best wishes,

Mark

       
Yuri's avatar
Yuri
273 posts
16 years ago
Yuri's avatar Yuri

Mark, but here’s an example usage let’s assume you have a page with list of weblog entries on a single-page entry and you want to highlight the titles of previous and next entry. Let’s also assume you supply entry_id in URL

the code would look like

{exp:weblog:entries entry_id="{segment_3}"}
{body}
{/exp:weblog:entries}
Now list all entries:
<ul>
{exp:weblog:entries entry_id="{segment_3}"}
<li>
<a href="http://{path=site/{entry_id}}">{title}</a>
{if "{entry_id}"=="{exp:math:subtract num1="{segment_3}" num2="1"}"} - this is prev entry
{if "{entry_id}"=="{exp:math:add num1="{segment_3}" num2="1"}"} - this is next entry
</li>
{/exp:weblog:entries}
</ul>

Note that I did not check this example yet

ANOTHER EXAMPLE I used the plugin to make prev/next links for Solspace Gallery Extended. I have only one gallery, but the pictures are linked to different entries, so regular prev/next linking does not work for me. So I have in CSS

.navlink {
  display: none;
}

and in template

{exp:gallery_extended:entries related_id="{segment_4}" type="weblog" disable="member_fields|date_fields"}
{if gallery_entry_id < segment_3}
<a href="http://{path=" class="navlink">prev</a>
{/if}
{if gallery_entry_id == segment_3}
[removed]
$(document).ready(function() {
$('#navlink{exp:math:subtract num1="{count}" num2="1"}').css('display', 'inline');
$('#navlink{exp:math:add num1="{count}" num2="1"}').css('display', 'inline');
});
[removed]
{/if}
{if gallery_entry_id>segment_3}
<a href="http://{path=" class="navlink">next</a>
{/if}
{/exp:gallery_extended:entries}

Note that you must use jQuery, have image ID in segment_3 and related weblog entry in segment_4

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Ah I kind of see some uses for this now although not entirely sure the next / previous links would work in all cases seeing as how most sites have more than one weblog so just taking off or adding one to the current entry_id wouldn’t necessarily mean that you would end up with the id of the previous or next entry in that weblog.

Having said that I think I can see a couple of uses for this in the future so thanks for giving this away to the community.

Best wishes,

Mark

       
Yuri's avatar
Yuri
273 posts
16 years ago
Yuri's avatar Yuri

Well, that’s merely example, not ‘real-world’ script. The second Gallery example is a ‘real-world’ solution that I use at one of my sites. Hope people will find even more ways to use it.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Yep I see what you mean. Thanks for the examples as they were definitely very helpful and I’m sure a lot of people will find this very useful.

Best wishes,

Mark

       
Les McKeown's avatar
Les McKeown
133 posts
16 years ago
Les McKeown's avatar Les McKeown

This is very helpful, yurrua - thanks for doing it - it will help me replace a lot of ugly php in my code.

Any chance of getting SUM and AVERAGE in there? 😊

       
Yuri's avatar
Yuri
273 posts
16 years ago
Yuri's avatar Yuri

Les, I might implement those later - or you can do it yourself, the plugin code is very simple

       
Les McKeown's avatar
Les McKeown
133 posts
16 years ago
Les McKeown's avatar Les McKeown

Thanks, Yurrua - I’ll give it a try…:)

       
vipinho's avatar
vipinho
18 posts
16 years ago
vipinho's avatar vipinho

Hi, I’m using it on my photoblog and it’s awesome. Maybe in the next update you could add increment options like {exp:math:increment num1="1" num2="9"} being num1 where you start and num2 where you end. Keep up the good work and thanks!

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen
Hi, I’m using it on my photoblog and it’s awesome. Maybe in the next update you could add increment options like {exp:math:increment num1="1" num2="9"} being num1 where you start and num2 where you end. Keep up the good work and thanks!

Depending on what you’re trying to do then the Loop Plugin might help you out on that side of things.

       
vipinho's avatar
vipinho
18 posts
16 years ago
vipinho's avatar vipinho

YES, you guys are great!That’s exactly what i needed.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen
YES, you guys are great!That’s exactly what i needed.

No problem 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.