Digiboy Posted February 28, 2013 Share Posted February 28, 2013 Hi guys, I keep getting error for this function: this is the error: Warning: Division by zero in...../..../rating.php and this is my code tried everything I could think of but no success <?php include ('db.php'); function getRating($id,$rating,$click){ $startCount= round($rating/$click); $blankCount=5-$startCount; $ratingHTML=''; for($i=0;$i<$startCount;$i++){ $rate=$i+1; $ratingHTML.='<img src="images/star.png" onclick=changeRating("'.$id.'","'.$rate.'")>'; } for($i=$startCount;$i<5;$i++){ $rate=$i+1; $ratingHTML.='<img src="images/white.png" onclick=changeRating("'.$id.'","'.$rate.'")>'; } return $ratingHTML; } ?> Could you please help m with this? I really appreciate your time in advance. Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/ Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 Make sure you don't call getRating() with a third argument that equals zero. Or edit the function so that it handles the $click==0 case gracefully. Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415501 Share on other sites More sharing options...
Digiboy Posted February 28, 2013 Author Share Posted February 28, 2013 Thanks but it is getting the click from database and I have changed all click fields to 1 so there is nothing equal to zero but still same result. Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415507 Share on other sites More sharing options...
Jessica Posted February 28, 2013 Share Posted February 28, 2013 That doesn't mean $click is not 0 at that point. Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415508 Share on other sites More sharing options...
Digiboy Posted February 28, 2013 Author Share Posted February 28, 2013 I dont understand, this is the code from here http://www.91weblessons.com/php-cookie-ajax-based-rating-script/ Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415511 Share on other sites More sharing options...
AyKay47 Posted February 28, 2013 Share Posted February 28, 2013 Perhaps if you post the context in which this function is being used we can help you further. Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415518 Share on other sites More sharing options...
Solution teynon Posted February 28, 2013 Solution Share Posted February 28, 2013 ... Again I post "http://www.whathaveyoutried.com" Since you seem to be looking for the quick fix for the error, change the line of code to this: $startCount = (!empty($click)) ? round($rating/$click) : 0; Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415530 Share on other sites More sharing options...
91weblessons Posted February 28, 2013 Share Posted February 28, 2013 Hi I m from 91weblessons I guess u r facing a problum in my rating script. So still u r facing any problem than let me know Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415553 Share on other sites More sharing options...
Digiboy Posted February 28, 2013 Author Share Posted February 28, 2013 ... Again I post "http://www.whathaveyoutried.com" Since you seem to be looking for the quick fix for the error, change the line of code to this: $startCount = (!empty($click)) ? round($rating/$click) : 0; Thank you very much you all especially Teynon Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1415634 Share on other sites More sharing options...
Crift Posted September 10, 2013 Share Posted September 10, 2013 Hello, I was wondering if you could also propose something for me as well regarding this issue. Here is the code that produces the error: $ratings = $jdb->loadObjectList(); $rating = ( $ratings[0]->rating_sum / $ratings[0]->rating_count ); $rating = round($rating, 0); if ( count ( $ratings ) > 0 ) { blah blah blah I can see the problem, only I don't know how to fix it. Any assistance is greatly appreciated. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1448953 Share on other sites More sharing options...
Crift Posted September 10, 2013 Share Posted September 10, 2013 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1448995 Share on other sites More sharing options...
vinny42 Posted September 10, 2013 Share Posted September 10, 2013 What is the problem? Also division by zero? if so: how about checking whether $ratings[0]->rating_count is a non-zero value? The solution to the original question cheats with !emoty(), which would allow "hello" as a valid division, which would still give an error. Look at ctype_digit() in the manual to verify that you are trying to divide by an integer, and simply do an IF to catch the error situation. Quote Link to comment https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/#findComment-1449018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.