I am working on presenting some threadfield results in a star format (like the rating system) and I am struggling on the rounding of the outputted values. I need them to be a number like 1.5 or 2 or 2.5 etc, but I cannot figure out the correct conditional to do this.
This is my code so far:
Code:
<setvar q>$GLOBALS['threadfields']['pfrating1']+$GLOBALS['threadfields']['pfrating2']+$GLOBALS['threadfields']['pfrating2']</setvar>
|
The values for the threadfields are 3, 3 and 2 which gives me a result of 8.
I then divided 8 by 3
Code:
<setvar r>$tplvars['q']/3</setvar>
|
and that gave me a result of 2.66666667 and it is this number I want to round to 2.5.
I tried the code below but it just gave no output. I was hoping it would round it to the decimal point.
Code:
<?=round("$tplvars['r']",1)?>
|
All help appreciated.
is invalid PHP, you probably meant
But that would display 2.7 in your example. If you want to round to the nearest 0.5, you've got to do something a bit more elaborate:
None of those seem to work (ie no output); I shall check with the site owner if he has uploaded all the plugin files.
I tested the example from the first post and that also did not work:
Code:
<setvar user51>get_user(51)</setvar>
<func htmlspecialchars_uni>{$tplvars['user51']['username']}</func>
|
He has XThreads installed, so maybe the phptpl_allowed_funcs .txt file is missing and it is not immediately apparent until I try to do a bit more?
Depending on the template, you may need to use $GLOBALS['tplvars'] instead of $tplvars
If you suspect a missing phptpl_allowed_funcs.txt, you could simply test using a simpler expression, eg
Code:
<setvar a>"blah"</setvar>
{$tplvars['a']}
|
The simpler expression is working, but the one in the forumdisplay thread template is not. I tested by deactivating XThreads and then using the simpler expression you provided in the header. I got the output blah.
This is my code from the forumdisplay_thread, all threadfields values at 4:
Code:
<li><label>Author's Rating: </label><setvar r>$GLOBALS['threadfields']['pfrating1']+$GLOBALS['threadfields']['pfrating2']+$GLOBALS['threadfields']['pfrating2']</setvar>
<setvar q>$tplvars['r']/3</setvar>
{$tplvars['q']}
{$tplvars['r']}
123<?=substr("987654321", 3)?>
<?=round($tplvars['q']*2)/2?>
</li>
|
The output is:
4 (correct)
12 (correct)
123 - NOT correct
blank
I am not sure how to use globals instead, but as the example from the first post in the Template Conditionals download failed then I am stumped. I am using XThreads custom fields in a prefixed template.
I tested this expression 123<?=substr("987654321", 3)?> in the header - and it returned 123.
Okay, that suggests that functions aren't working, ie the .txt file possibly isn't there.
Ok, thank you, I shall wait for a reply from the site owner (I can't access the FTP).
EDIT: I got a reply - the file was missing and he has uploaded it, and thank you for that better rounding code, it works beautifully.
SETUP: Users rate products based on different criteria and these ratings (option buttons values 1 to 5) are converted to a star rating with an average value at the end for the whole product.
The approach I used was to count the number of rating criteria (questions) per product and divide the sum of the criteria rating values with the count. This gave an average value.
It now seems that the users would like to choose to keep some values blank, i.e. select "not set" when faced with the option buttons, but not to always do so. I have that working fine, but the members would also like to see an average value for the overall rating and I cannot figure out how to get a count of ONLY the filled values (eg a value greater or equal to 1) and use that to divide the sum.
This is the code I have so far:
PHP Code:
<setvar rq1>$GLOBALS['threadfields']['pfrating1']['value']</setvar>
<setvar rq2>$GLOBALS['threadfields']['pfrating2']['value']</setvar>
<setvar rq3>$GLOBALS['threadfields']['pfrating8']['value']</setvar>
<setvar rq4>$GLOBALS['threadfields']['pfrating10']['value']</setvar>
<setvar rq5>$GLOBALS['threadfields']['pfrating11']['value']</setvar>
<if ($GLOBALS['threadfields']['pfrating12']['value']) >= 1 then>
<setvar rq6>$GLOBALS['threadfields']['pfrating12']['value']</setvar>
</if>
<if ($GLOBALS['threadfields']['pfrating13']['value']) >= 1 then>
<setvar rq7>$GLOBALS['threadfields']['pfrating13']['value']</setvar>
</if>
<setvar rq8>$GLOBALS['threadfields']['pfrating29']['value']</setvar>
<setvar tr>$tplvars['rq1']+$tplvars['rq2']+$tplvars['rq3']+$tplvars['rq4']+$tplvars['rq5']+$tplvars['rq6']+$tplvars['rq7']+$tplvars['rq8']</setvar>
<if ($tplvars['rq7']) < '1' && ($tplvars['rq6']) < '1' then>
<setvar count>6</setvar>
<elseif ($tplvars['rq7']) < '1' || ($tplvars['rq6']) < '1' then>
<setvar count>7</setvar>
<else>
<setvar count>8</setvar>
</if>
<setvar or>$tplvars['tr']/$tplvars['count']</setvar>
|
As you can see, that is looking terribly clunky and I wondered if there was a way to get the count instead of all the if else steps (which will be horrific if more than 2).
Code:
<setvar ratings>array_filter(array($GLOBALS['threadfields']['pfrating1']['value'], $GLOBALS['threadfields']['pfrating2']['value'], ...))</setvar>
Average: <?=count($GLOBALS['tplvars']['ratings']) ? array_sum($GLOBALS['tplvars']['ratings']) / count($GLOBALS['tplvars']['ratings']) : 0 ?>
|
No clue whether it works.
I tested it (thank you) and as it is...it did not work.
I broke the formula down a bit and found that the array filter returns a value of 1, when I ran the whole formula the returned value for 8 fields was 3.75.
The inputs were 6 * 5 and 2 * no value. which is 30, so an average of 3.75 means that all fields were being counted whether filled in or not.