Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Good evening all!

I have a an editable of type text in a DBF.
Code: Select all
<cms:editable name='vals' type='text' />


In which I enter values as:
23945+475d


To output it I am doing something as:
Code: Select all
<cms:each vals sep='+'>
    <cms:show item /><br />
</cms:each>


Now I want to access the item individually. So I went ahead and dumped the values using:
Code: Select all
<cms:each vals sep='+'>
    <cms:dump />
</cms:each>


and got the output as:
each
k_total_items: 2
k_count: 0
k_first_item: 1
k_last_item: 0
key: 0
item: 23945

each
k_total_items: 2
k_count: 1
k_first_item: 0
k_last_item: 1
key: 1
item: 475d



My question is:
How can I access only the Item 23945 or 475d individually?
Is it possible using the key?
In short, is it possible to access value individually? If Yes, How?

Regards,
GenXCoders
Image
where innovation meets technology
I would do:

Code: Select all
<cms:set vals="23945+475d" />
<cms:each vals sep='+'>
    <cms:if k_first_item>
        <cms:set first_part=item scope='global' />
    </cms:if>
    <cms:if k_last_item>
        <cms:set second_part=item scope='global' />
    </cms:if>
</cms:each>

First Part: <cms:show first_part /><br>
Second Part: <cms:show second_part /><br>
My variant -
Code: Select all
<cms:set vals="23945+475d" />
<cms:set vals_arr = '[]' is_json='1' />
<cms:each vals sep='+'>
    <cms:set vals_arr. = item scope='parent'/>
</cms:each>

First Part: <cms:show vals_arr.0 /><br>
Second Part: <cms:show vals_arr.1 /><br>
@Blutbaden: I had done something like this. Thanks for the revert.
@trendoman: This I find as a more effective way to tackle the problem by using the key directly.
Image
where innovation meets technology
genxcoders wrote: a more effective way to tackle the problem by using the key directly.

That was the question, wasn't it? Also helpful if string knowingly has more than 2 items..

A more generic approach would be a function (cms:func) which accepts a string, a separator and a key and returns needed part. Advancement lies in function's parameter key, which can be programmed to take either numerical value - 0,1, 2, etc., or take keywords - last, first.
Sample declaration and call could be like -
Code: Select all
<cms:func 'getStringPart' string='' separator='' key='' >..</cms:func>

<cms:call 'getStringPart' vals '+' 'last' />
<cms:call 'getStringPart' vals '+' '0' />
5 posts Page 1 of 1
cron