Page 1 of 1

Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 11:19 am
by blackrain
I have looked everywhere but to no avail, I am trying to find a solution to taking and array of answers and deciding the the order in which they are scored, highest being number 1

current code below:

Code: Select all

{foreach from=$fld_177 item=task name=foo}
{assign var='sum_all_foreach' value=$sum_all_foreach+$task}
{/foreach}
{assign var='result' value=$smarty.foreach.foo.total}

{assign temp_text value=$fld_177}
{assign var=result value=$temp_text|replace:',':''|count_characters}

{assign var='array' value=[$fld_140,$fld_145,$fld_150,$fld_155,$fld_160,$fld_165,$fld_170] scope=global}
{$arrValueCounts=$array|array_count_values}

{assign var=times value=50}
{foreach $arrValueCounts as $k=>$v}
{if $k == 'D'}
{math equation="x*y+z" x=$v y=$times z=$fld_190  assign=dom}
Dominance = {$dom}<br />
{elseif $k == 'C'}
{math equation="x*y+z" x=$v y=$times z=$fld_205  assign=com}
Competence = {$com}<br />
{elseif $k == 'A'}
{math equation="x*y+z" x=$v y=$times z=$result  assign=att}
Attractivness = {$att}<br />
{elseif $k == 'S'}
{math equation="x*y+z" x=$v y=$times z=$fld_200  assign=soc}
Socio-Economics = {$soc}<br />
{elseif $k == 'W'}
{math equation="x*y+z" x=$v y=$times z=$fld_195  assign=war}
Warmth = {$war}<br />
{elseif $k == 'H'}
{math equation="x*y+z" x=$v y=$times z=$fld_210  assign=cha}
Charisma = {$cha}<br />
{elseif $k == 'V'}
{math equation="x*y+z" x=$v y=$times z=$fld_215  assign=vul}
Vulnarability = {$vul}<br />
{elseif $k == 'T'}
{math equation="x*y+z" x=$v y=$times z=$fld_184  assign=tru}
Trustworthyness = {$tru}<br />
{/if}
{/foreach}
The Form Builder collects the data and displays the results of the collected data.

7 questions with 4 answers for each.

there are 8 character traits that the answers are drawn from but not in every set of answers.

Then after the 7 questions are asked, there are 8 tie breakers that score specifically on each trait, then

The score from the trait questions is added to the answers from the first 7 questions and the results are displayed.

My issue is that I need to find the top 2 values and display them as 'primary' and 'secondary' traits.

it's complicated and I have everything up to the finding the top 2.

Code: Select all

{php}{/php}
and 
{$smarty->max($dom,$com,$att,$soc,$war,$cha,$vul,$tru)}
don't work, It looks like smarty does not have a Max equivalent for PHP.

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 11:30 am
by calguy1000
This should work:

Code: Select all

{$max=max($a,$b,$c);
You can call php functions within smarty. Though you may have to enable permissive smarty in the config.php to call this function.

If you have to do that... I would ensure that you are not allowing untrusted users to submit content and then display it without review.

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 11:37 am
by blackrain
Thanks, unfortunately that is one of the many possible solutions I have tried that failed to work.

I get "PHP function 'max' not allowed by security setting" error

not sure how to disable smarty security to check if the process works though.

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 12:11 pm
by calguy1000
In the config.php

Code: Select all

$config['permissive_smarty'] = 1;

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 2:09 pm
by blackrain
Great, that works for the 'primary', is there way to show the next number.

for if 250 is the top score and 200 is the second in the array how does max select that number?


many thanks

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 2:29 pm
by calguy1000
You asked about 'max'.

what you really want to do is sort your array.

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 2:34 pm
by blackrain
you are correct, the reason i asked about max is because, in all my searching 'max' was referenced as the function that gave the largest number. if there is a way to sort the array and the choose the top on as 'primary' and the the second a 'secondary', none of my searches resulted in that, I am at a loss.

I can sort the array but my knowledge is sadly lacking from that point on.

any help would be amazing, thanks


Mark

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 3:01 pm
by Rolf

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 3:17 pm
by calguy1000
This should work:

Code: Select all

{$tmp=[5, 3, 12, 8, 7]}
{asort($tmp)}
<pre>{$tmp|@print_r}</pre>

Re: Smarty alternative to php (max) function needed

Posted: Tue Aug 20, 2019 3:55 pm
by calguy1000
but I would just use {sort($arr)}

Re: Smarty alternative to php (max) function needed

Posted: Wed Aug 21, 2019 11:31 am
by blackrain
Thank you for the replies, Both solutions work well

many thanks

Mark