Smarty alternative to php (max) function needed

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
blackrain
Forum Members
Forum Members
Posts: 98
Joined: Wed Feb 20, 2008 4:33 pm

Smarty alternative to php (max) function needed

Post 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.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Smarty alternative to php (max) function needed

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
blackrain
Forum Members
Forum Members
Posts: 98
Joined: Wed Feb 20, 2008 4:33 pm

Re: Smarty alternative to php (max) function needed

Post 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.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Smarty alternative to php (max) function needed

Post by calguy1000 »

In the config.php

Code: Select all

$config['permissive_smarty'] = 1;
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
blackrain
Forum Members
Forum Members
Posts: 98
Joined: Wed Feb 20, 2008 4:33 pm

Re: Smarty alternative to php (max) function needed

Post 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
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Smarty alternative to php (max) function needed

Post by calguy1000 »

You asked about 'max'.

what you really want to do is sort your array.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
blackrain
Forum Members
Forum Members
Posts: 98
Joined: Wed Feb 20, 2008 4:33 pm

Re: Smarty alternative to php (max) function needed

Post 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
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: Smarty alternative to php (max) function needed

Post by Rolf »

- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Smarty alternative to php (max) function needed

Post by calguy1000 »

This should work:

Code: Select all

{$tmp=[5, 3, 12, 8, 7]}
{asort($tmp)}
<pre>{$tmp|@print_r}</pre>
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Smarty alternative to php (max) function needed

Post by calguy1000 »

but I would just use {sort($arr)}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
blackrain
Forum Members
Forum Members
Posts: 98
Joined: Wed Feb 20, 2008 4:33 pm

Re: Smarty alternative to php (max) function needed

Post by blackrain »

Thank you for the replies, Both solutions work well

many thanks

Mark
Locked

Return to “Modules/Add-Ons”