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

Help with creating a switch variable in a custom plugin…

Development and Programming

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

Hi there,

I was just wondering if someone could possibly explain the following snippet of code to me (hope it’s okay to post as I have seen it in other people’s modules etc)

/** ----------------------------------------
        /**  parse {switch} variable
        /** ----------------------------------------*/
            
            if (preg_match("/^switch\s*=.+/i", $key))
            
            {
            
                $sparam = $FNS->assign_parameters($key);
                $sw = '';
            
            if (isset($sparam['switch']))
            {
            
                $sopt = explode("|", $sparam['switch']);
                $sw = $sopt[($count + count($sopt)) % count($sopt)];
            
            }
            
                $tagdata = $TMPL->swap_var_single($key, $sw, $tagdata);
            
            }

I would like to use the {switch="1|2|3|4"} variable within a plugin that I am currently writing but not really too sure where to place this code to get it to work correctly. At the moment my plugin is looping all correctly and spitting out all other data that I want but if I place :

{exp:my_plugin}
{switch="1|2|3|4"} - {my_plugin_variable}
{/exp:my_plugin}

into my template even though my plugin loops fine and spits out all the item information that I want I only ever get 1 being spat out multiple times from the switch variable.

Anyone any ideas perhaps?

Thanks in advance.

Best wishes,

Mark

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

Hmm!!

I really should look more carefully before posting!!

Seems if I do this :

/** ----------------------------------------
        /**  parse {switch} variable
        /** ----------------------------------------*/
        foreach ($TMPL->var_single as $key => $val)
        {        
            
            if (preg_match("/^switch\s*=.+/i", $key))
            
            {
            
                $sparam = $FNS->assign_parameters($key);
                $sw = '';
            
            if (isset($sparam['switch']))
            {
            
                $sopt = explode("|", $sparam['switch']);
                $sw = $sopt[($count + count($sopt)) % count($sopt)];
            
            }
            
                $tagdata = $TMPL->swap_var_single($key, $sw, $tagdata);
            
            }
        
        }

It now all works!

Just wondering though am I doing this all correctly. Don’t want to carry on if it is indeed the wrong way to do this so would love to know before carrying on. Also if anyone is feeling really really generous it would be excellent to understand in lay-mens terms exactly what the code is doing?

Best wishes,

Mark

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
17 years ago
Pascal Kriete's avatar Pascal Kriete

It’s done like that in most of EE, so I would say you’re good to go. What part of the code do you have trouble with?

Very rough review:

foreach ($TMPL->var_single as $key => $val)

You probably know this, it loops through the array of single tags. $key is each variable’s name and $val is the full variable. So for each single variable it runs the checks inside.

if (preg_match("/^switch\s*=.+/i", $key))

Anything that doesn’t fit the pattern: switch[spaces]=[morethanonecharacter] does not get processed. Basically filters out all tags except for the switch one.

$sparam = $FNS->assign_parameters($key);

Gets the tag’s parameters and drops them into an array.

if (isset($sparam['switch']))

Makes sure that there is an actual switch parameter to process.

$sw = $sopt[($count + count($sopt)) % count($sopt)];

Take the $count (current iteration), divide it by the total possible and get the remainder. Then the remainder is used as the array key to get the switch value.

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

Hiya,

Thanks for that! Think I’m starting to understand a bit more now 😉

Best wishes,

Mark

       

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.