开发者

PHP Division by Zero [closed]

开发者 https://www.devze.com 2023-03-15 07:49 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have the following PHP code to display my tag cloud. It seems if I don't have at least two tags that are the same, I get a warning message saying Warnng: Division by zero (shown below).

Could some one please help me fix this code? Thank you!

<?php
// define variables
$fontSizeUnit = "px";
$maxFontSize = 40;
$minFontSize = 10;
$tagsToDisplay = 100;
$tagDivider = " ";

// font size range
$fontSizeSpread = $maxFontSize - $minFontSize;

// create blank arrays
$tagArray = array();
$tempTagArray = array();

// DB: get all public tags
$result = mysqli_query($conn, "SELECT Tags FROM blog WHERE Tags != '' AND IsPublic = 1 ORDER BY RAND()")
or die($dataaccess_error);

if(mysqli_num_rows($result) > 0 )
{
    // loop through results
    while($row = mysqli_fetch_array($result))
    {
        // split the results
        $tempStringArray = preg_split("/,/", $row['Tags']);

        // loop through all items of this string array
        for ($a = 0; $a < count($tempStringArray); $a++) 
        {
            // convert to lowercase
            $tempStringArray[$a] = strtolower($tempStringArray[$a]);

            // check if it exists in tag array
            if (empty($tagArray[$tempStringArray[$a]])) 
            {
                // if it doesn't exist, create it with value 1
                $tagArray[$tempStringArray[$a]] = 1;
            } 
            else 
            {
                // if it does exist, increase the value by 1
                $tagArray[$tempStringArray[$a]] += 1;
            }
        }
    }

    // store to temporary array and sort descending
    arsort($tagArray);
    $numberOfTags = 0;

    foreach ($tagArray as $key => $val) 
    {
        $numberOfTags++;

        if ($numberOfTags > $tagsToDisplay) 
        {
            break;
        }

        $finalTagArray[$key] = $val;
    }

    ksort($finalTagArray);

    $maxTagCount = max($finalTagArray);
    $minTagCount = min($finalTagArray);

    $tagCountSpread = 开发者_JS百科$maxTagCount - $minTagCount; // <<== Problem here...

    $unitsPerCount = $fontSizeSpread/$tagCountSpread;

    // function to calculate the font size
    function calcSize($thisTagCount) {

        // import necessary global variables
        global $minTagCount, $minFontSize, $fontSizeUnit, $unitsPerCount;

        // calculate font size
        $thisFontSize = $minFontSize+($unitsPerCount*($thisTagCount-$minTagCount));

        // round font size and add units
        $thisFontSize = round($thisFontSize) . $fontSizeUnit;

        // return font size
        return $thisFontSize;

    }

    // echo out the resulting tags
    $b = 1;
    foreach ($finalTagArray as $key => $val) 
    {
        echo "<a href='snippets-by-tags.php?tag=".urlencode($key)."' style='font-size: ";
        echo calcSize($val);
        echo "'>" . htmlentities($key) . "</a>";

        if($b != count($finalTagArray)) 
        {
            echo " " . $tagDivider . " ";
        }
        $b++;
    }
}
else
{
    echo '<a href="#">none found ...</a>';
}
?>


You should check if $tagCountSpread is 0, obviously dividing with 0 = infinite. (Hence you receive an error). This could be an quick fix, but you should really think of the proper solution for your application.

 if ($tagCountSpread <= 0) $tagCountSpread = 1;
 $unitsPerCount = $fontSizeSpread/$tagCountSpread;


Your problem is actually with this line:

$unitsPerCount = $fontSizeSpread/$tagCountSpread;

If $tagCountSpread is zero, this is a division by zero. That will happen when $maxTagCount and $minTagCount are the same.

You should guard against this:

if ($tagCountSpread != 0)
{
  $unitsPerCount = $fontSizeSpread / $tagCountSpread;
}
else
{
  // sensible recovery code
}


// ...

$minTagCount = min($finalTagArray);

if(($tagCountSpread = $maxTagCount - $minTagCount) === 0){

    // do something else when its zero; throw an exception, throw a party... whatever

}

// otherwise continue

$unitsPerCount = $fontSizeSpread/$tagCountSpread;

// ...


Perhaps your problem is the following line:

$unitsPerCount = $fontSizeSpread/$tagCountSpread;

And not the previous one. You should check if $tagCountSpread is NULL (0) and if not, do the division:

if($tagCountSpread != 0)
{
     $unitsPerCount = $fontSizeSpread/$tagCountSpread;
}else{
     ///something
}


PHP will show what line it happens on, figure out why it's dividing by zero and fix that (Probably using some conditionals).

0

精彩评论

暂无评论...
验证码 换一张
取 消