Thursday, 12 September 2013

PHP Division By Zero?

PHP Division By Zero?

Two hours ago the following script worked. Now for some reason, I'm
getting an error "Warning: Division by zero in _ on line 30". Here's the
script. Can anyone tell me what I'm doing wrong and how to correct it?
Basically this script is pulling data from two elements on another
website, dividing them to get a number which in turn is being used to set
the width of an element. Been troubleshooting this for a while. Thanks so
much in advance!
<?php
define("FFF_SIXDEGREES", "http://www.stayclassy.org/fundraise?fcid=257739");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, FFF_SIXDEGREES);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if(!($results = curl_exec($curl))) {
print("{ \"total\": \"$0.00\" }");
return;
}
$pattern = '/<li class="goalTitle">Raised so
far:<\/li>\s*<li>\$([\d\.,]+)<\/li>/';
preg_match($pattern, $results, $matches);
$total = $matches[1];
$total = str_replace(",", "", $total);
// printf("<h2 class=\"raised-total\">$%s</h2>", formatMoney($total, true));
$pattern2 = '/<li class="goalTitle">My
goal:<\/li>\s*<li>\$([\d\.,]+)<\/li>/';
preg_match($pattern2, $results, $matches);
$total2 = $matches[1];
$total2 = str_replace(",", "", $total2);
// printf("<h2 class=\"goal-total\">$%s</h2>", formatMoney($total2, true));
$diff = ($total/$total2) * 100; // THIS IS THE LINE OF CODE IN QUESTION
function formatMoney($number, $fractional=false)
{
if ($fractional) {
$number = sprintf('%.2f', $number);
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
echo "<div class=\"progress-header\" style=\"width:$diff%;\"><span
class=\"raised-amount\">$$total</span><span
class=\"goal-amount\">$$total2</span></div>";
?>

No comments:

Post a Comment