Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
I was recently trying out the Couch array feature and I decided to check if a string can be found in a list of arrays so i did this:
Code: Select all
 <cms:set test_var='nanowines@gmail.com' scope='global'/>

    <cms:capture into="test_array" is_json='1'>
{
            <cms:pages masterpage="users/index.php">
        "<cms:show k_page_name />" : "<cms:show k_page_title />" <cms:if "<cms:not k_paginated_bottom />" >,</cms:if>
    </cms:pages>
   }</cms:capture>

    <cms:each test_array >
        <cms:if item eq test_var>
            <cms:set msg='I found a match' scope='global' />
            <cms:else />
            <cms:set msg='I found no match' scope='global' />
        </cms:if>
    </cms:each>

    <cms:show msg />


However the output message is always "'I found no match", even when there is a match. Any help with why it's not working? Thank you
In your code, as <cms:each> iterates through the array, at a point the value is found and the 'msg' variable correctly gets set with success message; but now <cms:each> moves to the next item - which does not match and the 'msg' gets overwritten to show failure.

Please try the following instead -
Code: Select all
<cms:set msg='I found no match' scope='global' />

<cms:each test_array >
    <cms:if item eq test_var>
        <cms:set msg='I found a match' scope='global' />
    </cms:if>
</cms:each>

<cms:show msg />

As an alternative, you may try using <cms:arr_val_exists> to find the value in array -
Code: Select all
<cms:set test_var='nanowines@gmail.com' scope='global'/>

<cms:capture into="test_array" is_json='1'>
[
    <cms:pages masterpage="users/index.php">
        "<cms:show k_page_title />" <cms:if "<cms:not k_paginated_bottom />" >,</cms:if>
    </cms:pages>
]
</cms:capture>

<cms:if "<cms:arr_val_exists test_var in=test_array />">
    I found a match
<cms:else />
    I found no match
</cms:if>

Hope this helps.
Thank very KK. I keep learning new things from you everyday. God bless you
Thanks :) You are always welcome.
4 posts Page 1 of 1
cron