Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Rounding a template var.
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #1
Rounding a template var.
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.



[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 04-21-2013 05:15 PM by leefish.)
04-21-2013 04:42 AM
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: #2
RE: Rounding a template var.

Code:
round("$tplvars['r']",1)

is invalid PHP, you probably meant

Code:
round($tplvars['r'],1)


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:

Code:
round($tplvars['r']*2)/2


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



[Image: leelink.gif]
MYBB1.6 & XThreads
04-21-2013 06:17 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: #4
RE: Rounding a template var.
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']}


My Blog
04-21-2013 07:30 PM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #5
RE: Rounding a template var.
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:
1
2
3
4
5
6
7
8
9
<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.



[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 04-21-2013 08:23 PM by leefish.)
04-21-2013 08:19 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: #6
RE: Rounding a template var.
Okay, that suggests that functions aren't working, ie the .txt file possibly isn't there.

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


[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 04-22-2013 02:55 AM by leefish.)
04-21-2013 09:47 PM
Visit this user's website Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #8
RE: Rounding a template var.
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:
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
26
27
28
29
30
31
32
// get the values and put them in a var

<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>

// get the sum of the values

<setvar tr>$tplvars['rq1']+$tplvars['rq2']+$tplvars['rq3']+$tplvars['rq4']+$tplvars['rq5']+$tplvars['rq6']+$tplvars['rq7']+$tplvars['rq8']</setvar>

// start figuring out how many are filled in

<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>

// get the average

<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).



[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 05-05-2013 02:32 AM by leefish.)
05-05-2013 02:20 AM
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: #9
RE: Rounding a template var.

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.


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


[Image: leelink.gif]
MYBB1.6 & XThreads
05-10-2013 02:36 AM
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: