Not sure if this is possible, or even logical…
But say for example i have a type EE entries tag that displays 6 entries. I then have a second entries tag that displays another 9 entries from a different channel. and so on maybe for 5 or 6 channels.
My idea is that i want to create a plugin that keeps count of each entry, across each the entries tags. i.e 1-6 and then 7-16 for the next and so forth.
The problem that i am having is storing that value so that next time around it can be read in and and added to.
I am no pro-programmer, but i just cannot think of a sensible way to store this, or pass it on. Can anyone advise?
Hi,
And thanks for your response, but forgive my ignorance here what do you mean by a static variable? I assume you don’t mean within the function and class of the plugin as this will not carry forward to the next time it is run?
If you mean the default EE variable that can be created within the template how would you go about the addition of values, especially within other looped tags such as playa?
Hi Rick, i have read up more, and can see how it works within a class on a function that is repeated. But it seems this does not play nice within EE For example:
class My_count
{
function get_count()
{
static $count = 0;
return $count++;
}
}
echo My_count::get_count();
echo My_count::get_count();
echo My_count::get_count();
echo My_count::get_count();Does output 0123
But when i use the same as an EE plugin and effectively repeat call that in the form of:
{exp:my_count::get_count}It always returns 0000
So am i being especially dumb here, or is EE doing something with that static variable that i do not know about?
For example, every time this plugin is called, the $count variable is incremented.
class Myplugin {
var $return_data;
static $count = 0;
function Myplugin()
{
Myplugin::$count++;
$this->EE =& get_instance();
$tagdata = $this->EE->TMPL->tagdata;
foreach ($this->EE->TMPL->var_single as $key => $val)
{
if ($val == 'count')
{
$tagdata = $this->EE->TMPL->swap_var_single('count', Myplugin::$count, $tagdata);
}
}
$this->return_data = $tagdata;
}
}If you used this plugin like this the count that is displayed would be incremented:
{exp:myplugin}
{count}
{/exp:myplugin}
{exp:myplugin}
{count}
{/exp:myplugin}Displays: 1 2
I see what you are doing there - but forgive me as i have not played with it yet… But i tried something similar. Are you not setting count to zero, adding one to it (++) and then getting the count variable back from the template, adding them together, and outputting it again?
So effectively the accumulating number is actually that fed from the template and not Myplugin::$count;
I’ll quickly test to see - as i need to modify a few bits as i was just using a single tag.
Hi Rick, Okay let me take a look, i am clearly just getting my head confused with this as it is late! Thanks for your help - i am sure if i pay around with the increment it should all make sense!
It is doing what i am after - but for some reason i just cannot work the logic of why! For example if i duplicate the line where you increment the count twice or three times, i am still only getting an increment of 1. This must be somehting to do with the way static works - but i don;t quite grasp it yet!
***Edit - likewise - if i edit out the increment on the count it does still increment???
//Myplugin::$count++;Thanks, N
Well, this is a scaled back version of another plugin that used dynamic parameters. I think EE is caching it instead of executing it every time. I haven’t tested this code, but this thread has the same caching issue: http://ellislab.com/forums/viewthread/138970/
Edit: You’d have to use the plugin with different dynamic parameters on every call or within a channel entries loop for example where the data within changes on every iteration. Sorry about that. Hope you can get this to work.
Edit: Aha, from that link: “Second, If the plugin’s tag has the word “random” in it, it won’t have this caching performed, otherwise it will.”
Try this:
{exp:myplugin random="true"} {count} {/exp:myplugin}
Sorry, this is me being silly, the {count} tag is of course picking up EE’s default count tag… hence the correct out put when in a entries tags! When i update ti to something else, nothing is output at all - the tag is not replaced. Again probably due to code from EE1 vs EE2 or somehting sill i hav done. Fresh eyes tomorrow and play again i think!
No, that was me being silly using a {count} tag.
Anyway, I changed the variable name to “counter” and verified this works:
<?php
class Myplugin {
var $return_data;
static $counter = 0;
function Myplugin()
{
Myplugin::$counter++;
$this->EE =& get_instance();
$tagdata = $this->EE->TMPL->tagdata;
foreach ($this->EE->TMPL->var_single as $key => $val)
{
if ($val == 'counter')
{
$tagdata = $this->EE->TMPL->swap_var_single('counter', Myplugin::$counter, $tagdata);
}
}
$this->return_data = $tagdata;
}
}So this template code:
{exp:myplugin random="true"}
{counter}
{/exp:myplugin}
{exp:myplugin random="true"}
{counter}
{/exp:myplugin}Outputs this:
1 2Hi Rick, Finally got a chance to start looking at this again, and in short, the overall problem was that random=”true” Without that all my previous code examples failed, add that in and they work - grrrr…
Guess i need to read up more on the documentation about this (if i can find it!).
Thanks again for your help, N
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.