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

« Next Oldest | Next Newest »

Messages In This Thread
Rounding a template var. - leefish - 04-21-2013, 04:42 AM
RE: Rounding a template var. - ZiNgA BuRgA - 04-21-2013, 04:58 PM
RE: Rounding a template var. - leefish - 04-21-2013, 06:17 PM
RE: Rounding a template var. - ZiNgA BuRgA - 04-21-2013, 07:30 PM
RE: Rounding a template var. - leefish - 04-21-2013, 08:19 PM
RE: Rounding a template var. - ZiNgA BuRgA - 04-21-2013, 09:39 PM
RE: Rounding a template var. - leefish - 04-21-2013, 09:47 PM
RE: Rounding a template var. - leefish - 05-05-2013 02:20 AM
RE: Rounding a template var. - ZiNgA BuRgA - 05-05-2013, 10:09 PM
RE: Rounding a template var. - leefish - 05-10-2013, 02:36 AM
RE: Rounding a template var. - ZiNgA BuRgA - 05-10-2013, 09:37 AM
RE: Rounding a template var. - leefish - 05-10-2013, 06:04 PM
RE: Rounding a template var. - ZiNgA BuRgA - 05-12-2013, 11:04 AM
RE: Rounding a template var. - leefish - 05-12-2013, 11:48 PM
RE: Rounding a template var. - ZiNgA BuRgA - 05-13-2013, 02:11 PM
RE: Rounding a template var. - leefish - 05-13-2013, 09:25 PM
RE: Rounding a template var. - ZiNgA BuRgA - 05-14-2013, 09:34 AM
RE: Rounding a template var. - leefish - 05-14-2013, 06:48 PM

 Standard Tools
Forum Jump: