3063 Posts in 864 Topics by 646 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1393 Views |
-
[SOLVED] Average Rating Function Causing Errors

29 August 2009 at 9:03am Last edited: 1 September 2009 1:36am
I have a product rating system in place and I calculate average rating on approved ratings. If a product page doesn't have a rating or doesn't have an approved rating I get a White Screen of Death unless I remove the $AverageRating tag from the template.
Below is the Code
function AverageRating() {
$records = DataObject::get("Review","Approval = 1 AND ProductID =" . $this->ID);
$ratingArray = $records->column('Rating');
$totalRatingsCount = $records->Count();
$totalRatingSum = array_sum($ratingArray);
$averageRating = $totalRatingSum/$totalRatingsCount;
if ($averageRating >= ($half = ($ceil = ceil($averageRating))- 0.5) + 0.25) return $ceil;
else if($averageRating < $half - 0.25) return floor($averageRating);
else return $half;
}I've tried wrapping the rating calculation in in a if (isset($records)) {...}else{return 0;} but that doesn't help
-
Re: [SOLVED] Average Rating Function Causing Errors

29 August 2009 at 7:22pm
have you tried just adding this simple if after the dataobject get -
rather then the isset() which may return true if $records is empty dataobjectset.if(!$records || $records->Count <= 0) return false;
-
Re: [SOLVED] Average Rating Function Causing Errors

21 January 2010 at 8:36pm
Hi would you be able to please post the complete code to allow rating of a dataobject?
Thanks
Liam -
Re: [SOLVED] Average Rating Function Causing Errors

22 January 2010 at 2:45am
Sure, it needs the DataObjectManager installed. I've also set it up that a review needs to be approved before it's published but that can be easily removed.
-
Re: [SOLVED] Average Rating Function Causing Errors

22 January 2010 at 12:37pm
Awsome thanks Zenmonkey.
| 1393 Views | ||
|
Page:
1
|
Go to Top |



