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

How to get a plugin to loop?

Development and Programming

Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

This may be a dumb question, but how do I get a plugin to loop the way a module does.

For example, when you do a {exp:gallery:entries}, EE loops over the enclosing content for each image.

I would like my plugin to loop as well so that I list the sub-categories for a given category.

Here is my code, which only prints out the last category:

function Categoree()
    {
        global $DB, $IN, $TMPL, $FNS;
        
        if ( ! preg_match("#(^|\/)C(\d+)#", $IN->QSTR, $match))
        {    
            return '';
        }

        $query = $DB->query("SELECT exp_gallery_categories.cat_id, exp_gallery_categories.cat_name
                FROM exp_gallery_categories, exp_galleries
                WHERE exp_gallery_categories.gallery_id = exp_galleries.gallery_id AND 
                exp_gallery_categories.parent_id = '".$DB->escape_str($match['2'])."'");

        if ($query->num_rows == 0)
        {
            return '';
        }
        
        foreach ( $query->result as $row )
        {
            $tagdata = $FNS->prep_conditionals($TMPL->tagdata, $row);

            foreach ($TMPL->var_single as $key => $val)
            {
                switch ($val)
                {
                    case "cat_name":
                        $tagdata = $TMPL->swap_var_single($val, $row['cat_name'], $tagdata);
                        break;
                    case "cat_id":
                        $tagdata = $TMPL->swap_var_single($val, $row['cat_id'], $tagdata);
                        break;
                }
            }
        }
        $this->return_data = $tagdata;
    }

The query works fine and returns the correct values, but I am only seeing the last one on my page. My template code is:

{exp:categoree}
                        {cat_name} - {cat_id}
                    {/exp:categoree}
       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Look more closely at one of the module’s entries tags you are referring to. You will notice that a string is built, and appended to within each loop, which is used for the return data.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

I found the categories tag, which looks like it builds an array of data and then appends that to the $out var.

Still trying to figure it all out.

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Are you clear on what $TMPL->tagdata is?

$tagdata = $FNS->prep_conditionals($TMPL->tagdata, $row);

You’re resetting $tagdata within each iteration of the loop. You aren’t building a string that continues to add output in each loop. Removed from EE entirely, it’s the difference between:

$foo = 'whatever ';

for ($i = 0;$i < 10; $++)
{
    $bar = $foo;
}

echo $bar;

vs.

$foo = 'whatever ';

for ($i = 0;$i < 10; $++)
{
    $bar .= $foo;
}

echo $bar;
       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

Ya, I was just figuring that out!

Thanks for pointing it out.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

OK, for those of you following my oh-so-painful learning process, this seems to work:

$tagdata = '';
        
        foreach ( $query->result as $row )
        {
            $tagdata .= $FNS->prep_conditionals($TMPL->tagdata, $row);

            foreach ($TMPL->var_single as $key => $val)
            {
                switch ($val)
                {
                    case "cat_name":
                        $tagdata = $TMPL->swap_var_single($val, $row['cat_name'], $tagdata);
                        break;
                    case "cat_id":
                        $tagdata = $TMPL->swap_var_single($val, $row['cat_id'], $tagdata);
                        break;
                }
            }
        }
        $this->return_data = $tagdata;
       

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.