Forums / Setup & design / Counting in a loop and getting the value back

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