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

Is there a better way to assign variables than this?

Development and Programming

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

Hiya,

After making another plugin today I was wondering if there might be an easier way of doing something so I thought I would ask the fantastic people here in the forums 😊

In the plugin there is this kind of code shown below :

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

            if ($key == "days")
            {
                $tagdata = $TMPL->swap_var_single($key, $days_left, $tagdata);
            }
            if ($key == "hours")
            {
                $tagdata = $TMPL->swap_var_single($key, $hours_left, $tagdata);
            }
            if ($key == "minutes")
            {
                $tagdata = $TMPL->swap_var_single($key, $minutes_left, $tagdata);
            }
            if ($key == "event")
            {
                $tagdata = $TMPL->swap_var_single($key, $event, $tagdata);
            }
        }

All nice and simple and as there are only 4 variables being replaced not too difficult. Suppose now though I had a plugin that was replacing 30 or 40 so variables. There must be a much quicker way for me to do all of this surely?

I’m guessing perhaps if I could place all my names of my variables and what they should be replaced with into an array then that might do it perhaps?

Only thing is I’m not that fantastic with arrays so was wondering if that is indeed the best way to go about this or perhaps there is an even easier way?

I tried looking into the mod.weblog.php file to see if I could glean anything from that but I think I just confused myself even more as there are a lot of lines in that file!! 😊 😉 😊

So if anyone has an inkling of what I’m on about and can lend a hand then I would be very grateful. I’d really like to find some method so that I can exceptionally easily add in new variables at any time without having to write 3 lines of code every time not only to save a bit of time and code but also to make things a little neater in my plugins in the long run. I do have one plugin which I made which is replacing nearly 60 variables so you can guess what that part looks like! 😉

Best wishes,

Mark

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

Anyone?

I have a plugin that I’d really love to convert to be a lot easier not only on my eyes but also on my brain when I add more variables to it.

Best wishes,

Mark

       
ender's avatar
ender
1,644 posts
16 years ago
ender's avatar ender

well there’s always using switch/case instead of if/else… but that’s mostly just a syntax change and not really a solution to the long list of replacements.

in some cases it might work to create your variables in an associative array indexed by tag name:

$values["days"] = "2";
$values["hours"] = "48";
...

$tagdata = $TMPL->swap_var_single($key, $values[$key], $tagdata);
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Absolutely fantastic and exactly what I needed. Very very very much appreciated! 😊

Thanks for the help on this one, should hopefully save me a little RSI now!! 😉

Best wishes,

Mark

       
ender's avatar
ender
1,644 posts
16 years ago
ender's avatar ender

glad to help, as always.

-Ty

       

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.