4

I'm using phpstringfun plugin to check a comma-delimited list for the presence of a single number.

{exp:phpstringfun function="substr_count" par1="{member_group}"}
{member-groups}
{/exp:phpstringfun}

member_group is the member group of the current user. member-groups is a category custom field. I've confirmed they're both working with

 member-groups: {member-groups} member_group = {member_group}<br>

For example, I'll see the values "6,7" and "6", so 6 should be found in list. When I run the function, it always returns 0.

I've tried @dom-stubbs

{exp:channel:categories category_group="1" channel="pm" style="linear"}  

{exp:switchee variable="{member_group}" parse="inward"}  
  member-groups is: {member-groups}  
  {case value="{member-groups}"}  
    We have a match.  
  {/case}  
{/exp:switchee}  

{/exp:channel:categories}  

And no luck. I've confirmed that the member-groups and member_group variables are available. Nothing is output.

5
  • What data format is {member-groups} in? If it's still comma-delimited then that Switchee case is never going to match. It sounds as though you're just using a text field, in which case if you switch to pipe-delimited values my sample code should work.
    – Dom Stubbs
    Nov 27, 2012 at 19:32
  • @dom-stubbs: For some categories, {member-groups} is a pipe-delimited list, for others it's just a single digit. So it is "6|7" or just "6".
    – 4midori
    Nov 27, 2012 at 19:48
  • I've just tried this with some channel entries data and it seems to work as expected. One thought though - have you double checked that your category field has 'default text formatting' set as none? If not Switchee may be receiving <p> tags and other markup, which would definitely cause problems.
    – Dom Stubbs
    Nov 27, 2012 at 20:11
  • @dom-stubbs: I've doubled-checked that category fields has default text formatting as none. I've also stripped down the code, but Switchee doesn't seem to have access to {member-groups}. Would you be willing to take a look via a web conf? There's coffee/chocolate/karma in it for you. :) Email is bas ((at)) versastudio (dot] com
    – 4midori
    Nov 29, 2012 at 18:02
  • I made the mistake of testing with a channel entries variable rather than {member_group}. Having just done so I've spotted the problem and have updated my answer with a fix.
    – Dom Stubbs
    Nov 29, 2012 at 18:52

3 Answers 3

3

Although I'm unfamiliar with that plugin I doubt it's the best tool for this particular job. By the look of it that's essentially a wrapper for substr_count which is not going to work reliably in this context. For example, if your haystack was '243,423' and your needle was '2' the function would return 2, because the string contains two instances of the character '2'. Which is useless.

What you really want to do is check a value against an array. I'd suggest using Switchee. Assuming you can format {member-groups} as a pipe-delimited string (e.g. 8|241|33) you could do something like this:

{exp:switchee variable="global:member_group" parse="inward"}
    {case value="{member-groups}"}
        We have a match!
    {/case} 
{/exp:switchee}

Update: Having more closely replicated what you're trying to do I've realised that {member_group} is a late-parsed variable which means that Switchee does not have access to it (i.e. it receives the raw variable name every time). That's why you weren't getting any matches.

Fortunately Mark's Switchee developer branch has support for late-parsed global vars so there's a simple solution. Install that if you haven't already and change variable="{member_group}" to variable="global:member_group". I've updated my sample code accordingly.

0
3

Not sure of the underlying cause but a similar issue was reported in the comments for phpstringfun which remains unanswered: http://engaging.net/docs/phpstringfun#comment28

As others have advised I would look for another solution. You don't need to convert the {member-groups} field to be pipe-delimited to use switchee as you can set a case conditional to use regular expressions - see the sample code on devout:ee: http://devot-ee.com/add-ons/switchee

1
  • A pipe delimited string will be more efficient than a regular expression so ideally that's the better approach. Admittedly it's going to be pretty marginal in this case.
    – Dom Stubbs
    Nov 26, 2012 at 22:54
0

I'm wondering if it being across multiple lines is making a difference. Does it work correctly if formatted as...

{exp:phpstringfun function="substr_count" par1="{member_group}"}{member-groups}{/exp:phpstringfun}

Instead?

1
  • Multiple lines isn't the issue, but thanks.
    – 4midori
    Nov 26, 2012 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.