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 plugin issue and multiple items in parameter…

Development and Programming

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

Hiya,

Hopefully a nice easy one this but I’ve been trying to get the answer all day and have been trying different things in a plugin all day long and nothing is working. I’m not going to attach the plugin code here as I think I’ve probably made a complete hash of it all now and there is simply way too much in there with all the extra lines I’ve been testing with so if I could possibly explain what I’m trying to do and see if someone can show me the error of my ways?

Let’s say I have a plugin called pi.mb_fetch_data.php. Let’s now say that I want to have three functions within that plugin called all_data, single_data and multiple_data so I would get three different plugin calls as per the following :

{exp:mb_fetch_data:all_data}

{/exp:mb_fetch_data:all_data}


{exp:mb_fetch_data:single_data group="1"}

{/exp:mb_fetch_data:single_data}


{exp:mb_fetch_data:multiple_data groups="1|2|3"}
Data ID - {data_id}

Data Name - {data_name}

Data Count - {data_count}

Count = {count}

{/exp:mb_fetch_data:multiple_data}

Above you can see that on the two plugin calls that need a parameter I have a group or groups parameter. The problem I’m having is with the multiple_groups plugin function (although I haven’t yet come to the all_data function yet 😉 I’m hoping that I won’t have a problem with that one though).

What I need to do here is to take each of the numbers in the parameter and then run a SQL query to see if any results are there for that ID. If there are then I want to replace each of the {data_id}, {data_name} and {data_count} ({data_count} is the amount of results returned each time) variables with some results from the SQL query.

This will loop for as many ID’s as there are in the plugin parameter.

The way I went about trying to do this was to first take the groups=”“ parameter values out and explode them on the | character so that I would end up with an array of values :

<?php

    // START
    class Mb_fetch_data
    {
    var $return_data = '';

        // Multiple Groups - START
        function multiple_groups()
        {
            global $TMPL, $DB, $FNS;
            $tagdata = $TMPL->tagdata;
            $groups = $TMPL->fetch_param('groups');
            $groups = explode("|", $groups);
            
            foreach($groups as $item)
            {
                $results = $DB->query("MY SELECT QUERY GOES HERE
                                        WHERE SOMETHING = '$item'");

                if ($results->num_rows == 0)
                {
                    $this->return_data .= $TMPL->no_results();
                }
                else
                {
                    $tagdata = $TMPL->swap_var_single('mb_group_count', $results->num_rows, $tagdata);
                    $tagdata = $TMPL->swap_var_single('mb_group_title', $results->row['mb_group_title'], $tagdata);
                    $tagdata = $TMPL->swap_var_single('mb_group_id', $results->row['mb_group_id'], $tagdata);
                }

                $this->return_data = $tagdata;
            }
                return $this->return_data;
        }


}
?>

Please note that the code above won’t work as a plugin as I’ve obviously taken a few things out of it, mostly the SQL query and a few other pertinent details but the actual lay of the code is the same so I’m thinking either my foreach code or perhaps the way I’m outputting the variables is somehow wrong?

When I run this code in a template though I only get the one set of results returned as though it’s not looping through all the IDs that were set in the plugin parameter.

I know that this is probably something really really silly that I’m doing here and so if anyone can see the error of my ways and and push me in the right direction then I’d much appreciate it, thanks.

Best wishes,

Mark

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

I’m now wondering if I may have confused with the post above so I think I’ll try to simplify what I’m after and hopefully it will make more sense.

I have a plugin that has different functions in it. One of these is as per this tag code :

{exp:mb_fetch_data:multiple_groups groups="1|2|3|4"}
Count - {count}

Data ID - {data_id}

Data Name - {data_name}

Data Count - {data_count}

{/exp:mb_fetch_data:multiple_groups}

Let’s say as an example we just had one ID in the parameter of the plugin. What I would like to do is this :

1 - Run a SQL query to see if there are any results for this ID. 2 - If there are results then the {count} variable is set to the number of results returned for this ID. 3 - Replace all the custom variables in between the plugin with data from the SQL query.

That’s it.

The important part is that I want to do this for each ID in the parameter.

I haven’t thought about what I want to do if there are no results for a certain ID but I can come back to that at a later date I reckon 😉

Anyway hopefully that’s a little easier to understand now.

Thanks to anyone for any help on this.

Best wishes,

Mark

       
silenz's avatar
silenz
1,651 posts
16 years ago
silenz's avatar silenz

$tagdata is this:

Count - {count}

Data ID - {data_id}

Data Name - {data_name}

Data Count - {data_count}

After the first loop through:

$tagdata = $TMPL->swap_var_single('mb_group_count', $results->num_rows, $tagdata);
$tagdata = $TMPL->swap_var_single('mb_group_title', $results->row['mb_group_title'], $tagdata);
$tagdata = $TMPL->swap_var_single('mb_group_id', $results->row['mb_group_id'], $tagdata);

$tagdata is

Count - 1

Data ID - 7256

Data Name - Foobar

Data Count - 9

and on the second iteration there’s no variables to replace anymore. Also you overwrite $this->return_data with every loop instead of adding to it.

Get the point?

For a quick pointer, get fresh tagdata on every iteration and append each loops result to $this->return_data

foreach($groups as $item)
{
  $tagdata = $TMPL->tagdata;
  
  .....
  
  $this->return_data .= $tagdata;
}
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen
Get the point? For a quick pointer, get fresh tagdata on every iteration and append each loops result to $this->return_data
foreach($groups as $item)
{
  $tagdata = $TMPL->tagdata;
  
  .....
  
  $this->return_data .= $tagdata;
}

Fantastic! Thanks for that. Pretty sure I get it all now. I had actually placed in a

$this->return_data .= $tagdata;

but had never placed in the

$tagdata = $TMPL->tagdata;

inside the foreach loop so that was why!!

Don’t I feel silly now!

Thanks for that. Now on to the no_results conditional 😉

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.