MyBB Hacks

Full Version: Rounding a template var.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm assuming that when a field is "blank", it really is blank (or is 0).  If the value is something like "No Value" or similar, then it really isn't blank to my definition.

If the latter is what you're doing, you need to do an intval() call on every variable added to the array in the first line.
I thought that might be it; currently the field is set to have no default value and in the options list there is "not set". I was unsure if that equated to blank.

The custom field underlying type is text - should that be changed to intval?

I am also unsure how to do the intval call.
"(not set)" should be blank, don't know why it isn't in your case.
What happens if you output the variable directly?

intval version:

Code:
<setvar ratings>array_filter(array(intval($GLOBALS['threadfields']['pfrating1']['value']), intval($GLOBALS['threadfields']['pfrating2']['value']), ...))</setvar>
Average: <?=count($GLOBALS['tplvars']['ratings']) ? array_sum($GLOBALS['tplvars']['ratings']) / count($GLOBALS['tplvars']['ratings']) : 0 ?>

I put the below code in my template:

Code:
<setvar test>intval($GLOBALS['threadfields']['pfrating12']['value'])</setvar>
<setvar test1>intval($GLOBALS['threadfields']['pfrating1']['value'])</setvar>
<setvar myratings>array_filter(array(intval($GLOBALS['threadfields']['pfrating1']['value']), intval($GLOBALS['threadfields']['pfrating2']['value']), intval($GLOBALS['threadfields']['pfrating12']['value'])))</setvar>
Average: <?=count($GLOBALS['tplvars']['myratings']) ? array_sum($GLOBALS['tplvars']['myratings']) / count($GLOBALS['tplvars']['myratings']) : 0 ?>
Test: {$tplvars['test']}
Test1: {$tplvars['test1']}


The output for test (a "not-set") was 0.
The output for test1 (a value of 4) was 4.

The output for Average was 0.

I think - though I was unable to test it as I could not figure out how to write it - that the count is always returning zero, whether the values are filled in or not, and this is returning a value of zero for the average.

Oh, I forgot, array_filter doesn't work in Template Conditionals - it's disabled because it can be used to make arbitrary callbacks.

You could either enable it in phptpl_allowed_funcs.txt and take the security risk, or implement your own version of it by adding the following to inc/functions.php

PHP Code:
function array_filter_blank($array) { return array_filter($array); }

and adding array_filter_blank to phptpl_allowed_funcs.txt, then using array_filter_blank instead of array_filter


Also, why are you using "['value']" ?  It should work without that.  intval doesn't seem necessary - this works for me:

Code:
<setvar myratings>array_filter(array($threadfields['pfrating1'], $threadfields['pfrating12']))</setvar>
Average: <?=count($tplvars['myratings']) ? array_sum($tplvars['myratings']) / count($tplvars['myratings']) : 0 ?>

I added ['value'] because I was clutching at straws Tongue

I will see if I can add that line to the inc functions php as a patch.

One more question - will that Average output rounded like in my original question?
No, you'll need to add that in.
It all works, thank you very much indeed.

For those who may be interested, this was the final approach:

Code:
<setvar rq1>$GLOBALS['threadfields']['pfrating1']</setvar>
<setvar rq2>$GLOBALS['threadfields']['pfrating2']</setvar>
<setvar rq3>$GLOBALS['threadfields']['pfrating8']</setvar>
<setvar rq4>$GLOBALS['threadfields']['pfrating10']</setvar>
<setvar rq5>$GLOBALS['threadfields']['pfrating11']</setvar>
<setvar rq6>$GLOBALS['threadfields']['pfrating12']</setvar>
<setvar rq7>$GLOBALS['threadfields']['pfrating13']</setvar>
<setvar rq8>$GLOBALS['threadfields']['pfrating29']</setvar>
<setvar myratings>array_filter_blank(array($tplvars['rq1'],$tplvars['rq2'],$tplvars['rq3'],$tplvars['rq4'],$tplvars['rq5'],$tplvars['rq6'],$tplvars['rq7'],$tplvars['rq8']))</setvar>
<setvar or>count($tplvars['myratings']) ? array_sum($tplvars['myratings']) / count($tplvars['myratings']) : 0</setvar>

<ul class="pflist">
<li><label>Price: </label>
<ul class="star_rating reviewer" title="<?=round($tplvars['rq1'])?> of 5 stars">
			<li style="width: <?=round($tplvars['rq1']*2)?>0%;" class="current_rating"></li>
		</ul>

</li>
<li><label>Overall Rating: </label>
<ul class="star_rating reviewer" title="<?=round($tplvars['or']*2)/2?> of 5 stars">
			<li style="width: <?=round($tplvars['or']*2)?>0%;" class="current_rating"></li>
		</ul>

</li>
</ul>


I used the tplvars in the array filter because I have a lot of templates and this setup is suitable for using across all templates as a sort of code snippet; I am also using the variables I set for each field in the ratings later in the page.

Pages: 1 2
Reference URL's