Jump to content

PHP Warning: Division by Zero


Digiboy

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/275034-php-warning-division-by-zero/
Share on other sites

 

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

  • 6 months later...

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.