Forums / Developer / Test for Single Choice

Test for Single Choice

Author Message

David Jones

Friday 15 September 2006 3:31:38 am

I've got a single choice field in a defined class news.

The two optios are Yes / No.

I want to do something if yes and do somthing else if no. But I don't seem to be able to get the test to work.

This is the code I have

	{foreach $node.children as $newsFeatured}	
	    {if eq($newsFeatured.object.data_map.featured,'Yes')}
               do something
           {/if}
         {/foreach}

I've also tried:

{if eq($newsFeatured.object.data_map.featured,Yes)}
{if eq($newsFeatured.object.data_map.featured,'yes')}
{if eq($newsFeatured.object.data_map.featured,yes)}

How do I do this test?

Thanks

Claudia Kosny

Friday 15 September 2006 3:53:20 am

Hello David

Have a look at the attribute in question with
{$newsFeatured.object.data_map.featured|attribute('show', 2)}

There you will probably see that you are interested in the content of this attribute so fetch this one and inspect it the same way. There you can see what you have to compare with, most likely it is the number of the selected option. If you used a selectbox, the content contains an array where you have to fetch the first item if I remeber correctly.

Greetings from Luxembourg

Claudia

David Jones

Friday 15 September 2006 5:57:25 am

Thanks for your help claudia

{$newsFeatured.object.data_map.featured.content|attribute('show', 2)}

Outputs

Attribute 	Type 	Value
0 	           string 	1
Attribute 	Type 	Value
0 	           string 	0
Attribute 	Type 	Value
0 	           string 	1

So i've tried

{if eq($newsFeatured.object.data_map.featured,0)}
{if eq($newsFeatured.object.data_map.featured.content,0)}
{if eq($newsFeatured.object.data_map.featured.content|attribute,0)}

Interestingly the last one outputs all 3. The others have no output.

I just want to output the one with value 0.

Thanks again

Claudia Kosny

Friday 15 September 2006 8:00:50 am

Hi David

Does this
{$newsFeatured.object.data_map.featured.content|attribute('show', 2)}
output all 3 lines or just one for each time you call it up?
I hope it is the latter as otherwise I am not sure what datatype you use.

I will assumethat the output is just one of these lines.

Attribute       Type    Value
0                 string       1

This means that your attribute has an array as content which has one entry. This entry has the key 0, the value is of type string and has the content 1 (or 0 in one of your other lines).
This number is the key of your selection (basically the number of the entry in the selection counted zero based). If you would have a checkbox 1 would mean that it is selected, 0 not selected (although in case you would not have an array as content of the attribute but the number itself)

So to properly compare you have to access the first entry of this array. You can do that like this:

{$newsFeatured.object.data_map.featured.content[0]}

This is what you have to compare with.

Greetings

Claudia

David Jones

Friday 15 September 2006 8:57:25 am

Thanks again,

That was spot on, problem solved

eZ debug

Timing: Jan 18 2025 15:08:19
Script start
Timing: Jan 18 2025 15:08:19
Module start 'content'
Timing: Jan 18 2025 15:08:19
Module end 'content'
Timing: Jan 18 2025 15:08:19
Script end

Main resources:

Total runtime0.0165 sec
Peak memory usage2,048.0000 KB
Database Queries4

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0053 587.6875180.8438
Module start 'content' 0.00530.0042 768.531397.8516
Module end 'content' 0.00950.0070 866.382874.7031
Script end 0.0165  941.0859 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002012.1771120.0002
Check MTime0.00116.4306120.0001
Mysql Total
Database connection0.00073.935410.0007
Mysqli_queries0.003622.087040.0009
Looping result0.00000.140020.0000
Template Total0.006740.510.0067
Template load0.00074.496810.0007
Template processing0.005935.935310.0059
Override
Cache load0.00053.232610.0005
General
dbfile0.00042.6626100.0000
String conversion0.00000.018830.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs