Counting in a loop and getting the value back

Author Message

J-A Eberhard

Friday 03 March 2006 1:37:15 am

Hi,
I'm using a customized version of ezsurvey and wrote an additional test to display a different message if the answer given was correct or wrong (a correct answer is identified by a result value higher than 10)
The 2 piece of template code work fine, displaying 'correct' or 'wrong'.
I need to add a counter to be able to show at the end of the page the total number of answers tested as 'correct'. How can I include a counter, and bring it's value back to the first piece of code?

{section var=question loop=$survey_questions}
<div class="block">
{survey_question_result_gui view=item question=$question result_id=$result_id metadata=$survey_metadata}
</div>
{/section}
{$question.question_number}. {$question.text|wash('xhtml')}<br />
{$question.result_id}

{let result=fetch('survey', 'multiple_choice_result_item', 
							hash( 'question', $question, 
							'metadata', $metadata, 
							'result_id', $result_id ))}
							
  
{section var=ans loop=$result}
	{if $ans['value']|ge( 10 )}
	    <font color="#008000">{"correct"|i18n("design/gdp")}</font>  
    {else}
	    <font color="#ff0000">{"wrong"|i18n("design/gdp")}</font>
	{/if}
	{section show=$ans['label']|wash('xhtml')|count_chars}{$ans['label']|wash('xhtml')}
	{/section}
	{delimiter}, {/delimiter}
{/section}

Thanks a lot for an hint. has this to do with the variable scope?

Open Source Solution Provider
Open-Net Ltd Switzerland
http://www.open-net.ch

Nabil Alimi

Thursday 09 March 2006 9:03:28 am

Hi J-A,

What about using a simple counter ?

This could work that way :

{$question.question_number}. {$question.text|wash('xhtml')}<br />


{$question.result_id}


 

{let result=fetch('survey', 'multiple_choice_result_item', 

                                                        hash( 'question', $question, 

                                                        'metadata', $metadata, 

                                                        'result_id', $result_id ))}


                                                        


  
<b>{def $counter=0}</b>

{section var=ans loop=$result}


        {if $ans['value']|ge( 10 )}

        <b>{* INCREMENTING THE COUNTER *}</b>
        {set scope=global $counter=inc( $counter )}

           <font color="#008000">{"correct"|i18n("design/gdp")}</font>  

   {else}


           <font color="#ff0000">{"wrong"|i18n("design/gdp")}</font>


        {/if}


        {section show=$ans['label']|wash('xhtml')|count_chars}{$ans['label']|wash('xhtml')}


        {/section}


        {delimiter}, {/delimiter}


{/section}

You have {$counter} correct(s) answers.

For example.

My blog : http://www.starnab.com/ezpublish / http://www.starnab.com/ / http://www.assiki-consulting.com
eZ Publish Freelance developper. Feel free to contact me +33 674 367 057
nabil at assiki d0t fr

J-A Eberhard

Monday 13 March 2006 12:46:56 pm

Thanks a lot.
I took another approach by rewriting the {section} into {foreach} loop to avoid the scope problem.

Open Source Solution Provider
Open-Net Ltd Switzerland
http://www.open-net.ch

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 04:16:03
Script start
Timing: Jan 31 2025 04:16:03
Module start 'layout'
Timing: Jan 31 2025 04:16:03
Module start 'content'
Timing: Jan 31 2025 04:16:03
Module end 'content'
Timing: Jan 31 2025 04:16:03
Script end

Main resources:

Total runtime0.0160 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0049 588.2656151.2266
Module start 'layout' 0.00490.0024 739.492236.6797
Module start 'content' 0.00730.0070 776.171994.3125
Module end 'content' 0.01420.0017 870.484433.9922
Script end 0.0160  904.4766 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002414.9666140.0002
Check MTime0.00127.2115140.0001
Mysql Total
Database connection0.00074.435310.0007
Mysqli_queries0.002314.430430.0008
Looping result0.00000.076010.0000
Template Total0.00127.710.0012
Template load0.00106.084010.0010
Template processing0.00021.554910.0002
Override
Cache load0.00063.899110.0006
General
dbfile0.003220.203180.0004
String conversion0.00000.062640.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_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