1
nachenko
Can't check if a value is in an array
  • 2007/6/7 10:05

  • nachenko

  • Quite a regular

  • Posts: 356

  • Since: 2005/1/18


Hi!

I created an array in header.php file named "$xoops_usergroups", a list of groups the user belongs to. BTW, this is the code:

Quote:
$xoopsTpl->assign( 'xoops_usergroups', $xoopsUser->getGroups() );


Now I wanted to check it in smarty this way:

Quote:
<{if 16|in_array($xoops_usergroups)}>
....
<{/if}>


Check if someone belongs to group 16.

But guess what? It always returns TRUE, no matter what user checks it. I checked my test user belongs only to registered users group. I loged out and in, checked smarty console and values are the right ones.

In smarty forums, the syntax appeared like this:

Quote:
<{if 16|in_array:$xoops_usergroups}>
....
<{/if}>


But it can't be compiled, so i put array name between brackets. It compiled but gives always true.

So i used a foreach loop as a workaround, but i want to know why the former solution doesn't work.

What's wrong?

2
Dave_L
Re: Can't check if a value is in an array
  • 2007/6/7 11:54

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


I tried <{if 16|in_array:$xoops_usergroups}> and it seems to work properly, in spite of the warning message when the template is compiled.

But you could use another method.

.php file:

$xoopsTpl->assign('in_group'array_flip($xoopsUser->getGroups()));


template:

<{if isset($in_group[16])}>
In group 16
<{else}>
Not in group 16
<{/if}>

Login

Who's Online

352 user(s) are online (284 user(s) are browsing Support Forums)


Members: 0


Guests: 352


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits