Plugin: Mathematical operations
Posted: 01 December 2008 06:38 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  11-14-2007

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)

File Attachments
pi.math.zip  (File Size: 1KB - Downloads: 22)
Profile
 
 
Posted: 01 December 2008 07:13 AM   [ Ignore ]   [ # 1 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7061
Joined  04-15-2006

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 01 December 2008 07:25 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  11-14-2007

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="{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="{path=" class="navlink" id="navlink{count}">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="{path=" class="navlink" id="navlink{count}">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

Profile
 
 
Posted: 01 December 2008 07:33 AM   [ Ignore ]   [ # 3 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7061
Joined  04-15-2006

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 01 December 2008 07:38 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  11-14-2007

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.

Profile
 
 
Posted: 01 December 2008 07:54 AM   [ Ignore ]   [ # 5 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7061
Joined  04-15-2006

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 01 December 2008 09:18 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  183
Joined  03-31-2002

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? smile

 Signature 

Les McKeown, President & CEO
Predictable Success®

Profile
 
 
Posted: 01 December 2008 09:20 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  11-14-2007

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

Profile
 
 
Posted: 01 December 2008 09:51 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  183
Joined  03-31-2002

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

 Signature 

Les McKeown, President & CEO
Predictable Success®

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 66388 Total Logged-in Users: 43
Total Topics: 84713 Total Anonymous Users: 20
Total Replies: 454685 Total Guests: 192
Total Posts: 539398    
Members ( View Memberlist )