Forums / Developer / Setting the value of variables in a switch statement

Setting the value of variables in a switch statement

Author Message

joe hobbs

Monday 04 July 2005 3:29:17 am

Hello
I am using a drop down menu to allow a visitor to search for facilities in a region. Much of the solution to that was kindly provided by members of the eZ community on these boards. Now I want to modify the feature further. To achieve that I using a 'switch' statement which is in a html form. It seems simple enough, but I am not having any success. I suspect it is something to do with using a switch to set the value of a variable. I include the code and I wonder if any one could point me in the right direction? Incidently, if the switch statement were successful I would pass the value by method = 'get' to a template file to be used as an argument in a fetch.
As usual many thanks for your good attention.

{*I want to set the value of app depending on the page_ID*}
{let pageid=$node.node_id}
{let app=' '}
{switch name=pagetester match=$pageid}
{case match=72}
$app='petrol'
{/case}
{case match=71}
$app='pubs'
{/case}
{case}
default case
{/case}
{/switch}

Eivind Marienborg

Monday 04 July 2005 5:28:02 am

You have to use set-tags, like this:

{*I want to set the value of app depending on the page_ID*}
{let pageid=$node.node_id
            app=' '}
{switch name=pagetester match=$pageid}
{case match=72}
{set $app='petrol'}
{/case}
{case match=71}
{set $app='pubs'}
{/case}
{case}
default case
{/case}
{/switch}
{/let}

Also, it's a good thing to make it a habit to close your let's :)

joe hobbs

Monday 04 July 2005 6:35:59 am

Thank you Eivind
I edited the code with your suggestion (below). Unfortunately the value of the variable 'app' does not get reset to 'edu' in the switch and retains the initial value, 'test' when I output it. Logic tells me this should work, but what is missing?

{let pageid=$node.node_id app='test'}
{switch name=pagetester match=$pageid}
{case match=73}
        {set $app='edu'}
{/case}
{case}
        default
{/case}
{/switch}
{/let}

Eivind Marienborg

Monday 04 July 2005 6:45:50 am

Hmm, sorry, I can't seem to find any errors in your code there.. Anyone else?

Trevor Clowry

Wednesday 06 July 2005 8:05:18 am

I've had the same problem as this on several occasions and can't find the solution.
In a similar example to Joe Hobbs version I have tested the node_id before the switch statement. When the node id is equal to 73 it enters the case:

{case match=73}
        {set $app='edu'}
{/case}

However when i test the $app variable after the switch statement it is still set to its original value. It seems like the $app variable is not being set.
Can somebody try joe's code in a template file and see if it works?
Can anyone suggest a workaround to this?
Thanks again.
Trevor

joe hobbs

Friday 08 July 2005 8:27:39 am

To conclude on this post. I used an if statement to resolve the problem I experienced with the switch (see code below). If anyone knows why the switch would not set the value of the variable whereas the if statement will I would be most interested to learn.
Many thanks

{let pageid=$node.node_id app=''}
{if eq( $pageid, 73 )}
    {set $app='edu'}
{else}
    {set $app='default'}
{/if}


eZ debug

Timing: Jan 15 2025 05:47:24
Script start
Timing: Jan 15 2025 05:47:24
Module start 'content'
Timing: Jan 15 2025 05:47:24
Module end 'content'
Timing: Jan 15 2025 05:47:24
Script end

Main resources:

Total runtime0.3272 sec
Peak memory usage6,144.0000 KB
Database Queries134

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0108 685.8984214.7734
Module start 'content' 0.01080.0062 900.6719189.4766
Module end 'content' 0.01700.3101 1,090.14841,479.2891
Script end 0.3271  2,569.4375 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00692.1097200.0003
Check MTime0.00150.4605200.0001
Mysql Total
Database connection0.00120.379610.0012
Mysqli_queries0.265181.02361340.0020
Looping result0.00180.54471310.0000
Template Total0.309694.610.3096
Template load0.00300.916410.0030
Template processing0.306693.702010.3066
Override
Cache load0.00270.831010.0027
Sytem overhead
Fetch class attribute can translate value0.00110.332310.0011
XML
Image XML parsing0.00030.091110.0003
General
dbfile0.00040.1156140.0000
String conversion0.00000.003130.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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