Jump to content


Photo

PHP Warning: Division by Zero

division by zero php

Best Answer teynon, 28 February 2013 - 12:20 AM

... 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;
Go to the full post


  • Please log in to reply
11 replies to this topic

#1 Digiboy

Digiboy

    Member

  • Members
  • PipPip
  • 29 posts

Posted 27 February 2013 - 07:18 PM

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.

 



#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,136 posts
  • LocationWA

Posted 27 February 2013 - 07:33 PM

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.

#3 Digiboy

Digiboy

    Member

  • Members
  • PipPip
  • 29 posts

Posted 27 February 2013 - 07:50 PM

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. 



#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 27 February 2013 - 07:53 PM

That doesn't mean $click is not 0 at that point.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#5 Digiboy

Digiboy

    Member

  • Members
  • PipPip
  • 29 posts

Posted 27 February 2013 - 08:03 PM

I dont understand, this is the code from here http://www.91webless...-rating-script/



#6 AyKay47

AyKay47

    Sick!

  • Members
  • PipPipPip
  • 3,287 posts
  • LocationEast Coast, U.S.
  • Age:24

Posted 27 February 2013 - 08:32 PM

Perhaps if you post the context in which this function is being used we can help you further.


Hola!
I'm not going to hold your hand and write the code for you - ain't nobody got time for that!

#7 teynon

teynon

    Advanced Member

  • Members
  • PipPipPip
  • 895 posts

Posted 28 February 2013 - 12:20 AM   Best Answer

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

Support my Kickstarter Project!
http://www.kickstart...7618755/antroid

http://www.thomaseynon.com

Vulnerabilities: http://cwe.mitre.org...x.html#Guidance - MySQL.com hacked with SQL Injection - If it happened to them, it can happen to you.


#8 91weblessons

91weblessons

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 28 February 2013 - 04:42 AM

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


#9 Digiboy

Digiboy

    Member

  • Members
  • PipPip
  • 29 posts

Posted 28 February 2013 - 12:25 PM

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



#10 Crift

Crift

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 10 September 2013 - 06:15 AM

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!



#11 Crift

Crift

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 10 September 2013 - 10:53 AM

Anyone? 



#12 vinny42

vinny42

    Advanced Member

  • Members
  • PipPipPip
  • 414 posts

Posted 10 September 2013 - 01:47 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com