New Plugin: Modulo Operator
Posted: 14 October 2009 06:44 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  62
Joined  04-01-2008

I wrote a quick little plugin to calculate the modulo value and figured I’d share it here.

In computing, the modulo operation finds the remainder of division of one number by another.

Given two numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder, on division of a by n. For instance, the expression “7 mod 3” would evaluate to 1, while “9 mod 3” would evaluate to 0. Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands.

Parameters

1) Dividend (required)
2) Divisor (required)

{exp:modulo dividend="dividend" divisor="divisor"}

For example:

{exp:modulo dividend="3" divisor="3"} => 0
{exp
:modulo dividend="4" divisor="3"} => 1
{exp
:modulo dividend="5" divisor="3"} => 2
{exp
:modulo dividend="6" divisor="3"} => 0

Hope it saves you some time…..

Best,
David

File Attachments
pi.modulo.php.zip  (File Size: 2KB - Downloads: 101)
 Signature 

David Dexter
Technical Director
http://www.brilliant2.com

Profile
 
 
Posted: 15 October 2009 01:23 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  403
Joined  07-04-2007

I often use this (with php) to add classes to remove padding on the last image in a gallery in the weblog entries loop.

Can you use it like this (I haven’t tried)?

{if {exp:modulo dividend="{count}" divisor="3"} = 0}class="last"{/if}
Profile
 
 
Posted: 15 October 2009 10:54 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  62
Joined  04-01-2008

Personally I use the switch tag to handle that issue. If I have a row with 3 items and I want the third to have a certain class, ‘endrow’ for example, I would do something like this:

<div class="normalclass {switch='||endrow'}">
<
img src="path/to/image/" />
</
div>

The result would be:

<div class="normalclass">
<
img src="path/to/image/" />
</
div>

<
div class="normalclass">
<
img src="path/to/image/" />
</
div>

<
div class="normalclass endrow">
<
img src="path/to/image/" />
</
div>

Hope that helps!

Best,
David

 Signature 

David Dexter
Technical Director
http://www.brilliant2.com

Profile
 
 
Posted: 15 October 2009 01:27 PM   [ Ignore ]   [ # 3 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  10833
Joined  04-15-2006

First off - David thanks for a great plugin. I’ve done this in the past with PHP but it really annoys me doing things like that. I’ve always meant to make a plugin to do this and just never got around to it so this is great, thanks.

Joobs - 15 October 2009 01:23 AM

I often use this (with php) to add classes to remove padding on the last image in a gallery in the weblog entries loop.

Can you use it like this (I haven’t tried)?

{if {exp:modulo dividend="{count}" divisor="3"} = 0}class="last"{/if}

Should work if you do it like this :

{if "{exp:modulo dividend="{count}" divisor="3"}" == "0"}class="last"{/if}

Best wishes,

Mark

 Signature 

Shopping Cart Plugin
Full list of add-ons
———————————————————-
Buy me a drink, or two if you like!!

Profile
 
 
Posted: 18 October 2009 01:38 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  127
Joined  06-10-2007

omg this is so useful!

Thank you! smile

Profile
 
 
Posted: 24 February 2010 08:53 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  05-30-2009

OK I have found that neither of these solutions work…

Here is my code for both solutions. One using the new Modulo Operator.

Just to say the switch tag is not appropriate for my website layout.

If anyone could she some light it would be appreciated.

{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        
<!-- Link -->
        <
div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <
h1>{title}</h1>
            <
h3>{link_url}</h3>
            <
p>{link_description}</p>
        </
div>
        <!--
// Link -->
            
{if "{exp:modulo dividend="{count}" divisor="3"}" == "0"}
                
<div class="clear"></div>
            
{/if}
        {
/exp:weblog:entries}
{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        
<!-- Link -->
        <
div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <
h1>{title}</h1>
            <
h3>{link_url}</h3>
            <
p>{link_description}</p>
        </
div>
        <!--
// Link -->
            
{if count == 3 || count == 6 || count == 9 || count == 12 || count == 15 || count == 18 || count == 21}
                
<div class="clear"></div>
            
{/if}
        {
/exp:weblog:entries}
Profile
 
 
Posted: 24 February 2010 11:08 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  62
Joined  04-01-2008

Hey Marker,

If you output the tag outside of the if statement is it returning the desired result:

i.e.

{exp:modulo dividend=“3” divisor=“3”}

Thanks,
David

 Signature 

David Dexter
Technical Director
http://www.brilliant2.com

Profile
 
 
Posted: 24 February 2010 01:56 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  05-30-2009

Thanks for your reply, you will have to excuse me I am not a developer per say.

How do you mean can you show me?

M.

Profile
 
 
Posted: 24 February 2010 01:58 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  62
Joined  04-01-2008

If you put exactly this:

{exp:modulo dividend="3" divisor="3"}

in you template. What does it show… (It should show a zero)

 Signature 

David Dexter
Technical Director
http://www.brilliant2.com

Profile
 
 
Posted: 24 February 2010 03:25 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  05-30-2009

Yes it does, it’s shows a zero. What does this mean?

Thanks
M.

Profile
 
 
Posted: 24 February 2010 03:30 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  62
Joined  04-01-2008

Try this:

{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        
<!-- Link -->
        <
div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <
h1>{title}</h1>
            <
h3>{link_url}</h3>
            <
p>{link_description}</p>
        </
div>
        <!--
// Link -->
        
{switch="||<div class="clear"></div>"}
{
/exp:weblog:entries}
 Signature 

David Dexter
Technical Director
http://www.brilliant2.com

Profile
 
 
Posted: 24 February 2010 03:51 PM   [ Ignore ]   [ # 11 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  05-30-2009

well, it worked on part outputting ‘<div class=’ to every third entry.

Any ideas?

Profile
 
 
Posted: 24 February 2010 03:54 PM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  62
Joined  04-01-2008

Yeah, change the quotes to single quotes. I’d also add comments inside the div for better browser compatibility.

{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        
<!-- Link -->
        <
div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <
h1>{title}</h1>
            <
h3>{link_url}</h3>
            <
p>{link_description}</p>
        </
div>
        <!--
// Link -->
        
{switch="||<div class='clear'><!-- --></div>"}
{
/exp:weblog:entries}
 Signature 

David Dexter
Technical Director
http://www.brilliant2.com

Profile
 
 
Posted: 24 February 2010 04:09 PM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  05-30-2009

Yes that worked a treat many thanks.

I also tried a method using the if tag which was…

{if count == 3 || count == 6}
do this...
{/if}

This one was odd because it worked in some places in my site but not in others, and I could not understand why as the code was all identical.

Many thanks fro your help, much appreciated.

I would love to see the plug-in you created in action. Perhaps you should put up online somewhere some working examples so non developers like me can grasp what it can be used for?

M.

Profile
 
 
Posted: 05 March 2010 07:20 AM   [ Ignore ]   [ # 14 ]  
Grad Student
Rank
Total Posts:  98
Joined  03-13-2009
dpdexter - 24 February 2010 03:54 PM

Yeah, change the quotes to single quotes. I’d also add comments inside the div for better browser compatibility.

{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        
<!-- Link -->
        <
div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <
h1>{title}</h1>
            <
h3>{link_url}</h3>
            <
p>{link_description}</p>
        </
div>
        <!--
// Link -->
        
{switch="||<div class='clear'><!-- --></div>"}
{
/exp:weblog:entries}

Hmm - it’s not working for - it does not output any div clear?

Profile
 
 
Posted: 05 March 2010 08:37 AM   [ Ignore ]   [ # 15 ]  
Grad Student
Rank
Total Posts:  98
Joined  03-13-2009

with modulo plugin I’m using this

{exp:weblog:entries weblog="clients_logos" disable="categories|category_fields|member_data|pagination|trackbacks" show_future_entries="yes" show_expired="yes"}
        
<dl class="client-entry">
          <
a href="{if '{client_url}' != ''}http://www.{client_url}{if:else}#{/if}"><img src="{client_logo}" /></a>
        </
dl>
            
{if '{exp:modulo dividend="{count}" divisor="3"}' == '0'}
                
<div class="clear"></div>
            
{/if}    
    {
/exp:weblog:entries}

and it outputs div clear after every entry from my weblog
any ideas?

Profile
 
 
Posted: 05 March 2010 12:14 PM   [ Ignore ]   [ # 16 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  05-30-2009

viadyn,

I could not get the modulo plugin to work for me either but the ‘switch’ parameter solution works fine for me using the single quotes as suggested above. Can you not get this solution to work?

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 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120494 Total Logged-in Users: 42
Total Topics: 126565 Total Anonymous Users: 26
Total Replies: 665430 Total Guests: 286
Total Posts: 791995    
Members ( View Memberlist )