Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Rounding a template var.
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #11
RE: Rounding a template var.
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.

My Blog
05-10-2013 09:37 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #12
RE: Rounding a template var.
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.


[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 05-10-2013 06:04 PM by leefish.)
05-10-2013 06:04 PM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #13
RE: Rounding a template var.
"(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 ?>


My Blog
05-12-2013 11:04 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #14
RE: Rounding a template var.
I put the below code in my template:

Code:
1
2
3
4
5
6
<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.



[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 05-13-2013 12:03 AM by leefish.)
05-12-2013 11:48 PM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #15
RE: Rounding a template var.
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 ?>


My Blog
05-13-2013 02:11 PM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #16
RE: Rounding a template var.
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?


[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 05-13-2013 09:34 PM by leefish.)
05-13-2013 09:25 PM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #17
RE: Rounding a template var.
No, you'll need to add that in.

My Blog
05-14-2013 09:34 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #18
RE: Rounding a template var.
It all works, thank you very much indeed.

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

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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.



[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 05-14-2013 07:51 PM by leefish.)
05-14-2013 06:48 PM
Visit this user's website Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: