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
8 replies to this topic

#1 Digiboy

Digiboy

    Member

  • Members
  • PipPip
  • 11 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

    Still alive

  • Gurus
  • 4,545 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
  • 11 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

    The Web Mason

  • Gurus
  • 8,744 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
  • 11 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
  • 881 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
  • 11 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 






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