2011-06-23 44 views
0

我有以下PHP代碼來顯示我的標籤雲。看起來,如果我沒有至少兩個相同的標籤,我會收到一條警告信息,說Warnng:除以零(如下所示)。PHP除零

有人能幫我解決這個問題嗎?謝謝!

<?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 = $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>'; 
} 
?> 

回答

2

您應該檢查是否$tagCountSpread是0,0分顯然無限=。 (因此你收到一個錯誤)。這可能是一個快速解決方案,但您應該真的爲您的應用程序考慮適當的解決方案。

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

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

// ... 
4

你的問題實際上是這一行:

$unitsPerCount = $fontSizeSpread/$tagCountSpread; 

如果$ tagCountSpread是零,這是零師。當$maxTagCount$minTagCount相同時會發生這種情況。

你應該警惕這樣的:

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

也許你的問題是以下行:

$unitsPerCount = $fontSizeSpread/$tagCountSpread;

,而不是之前的一個。您應該檢查是否$tagCountSpread是NULL(0),如果不是,做師:

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

PHP將顯示它發生什麼行,找出爲什麼它除以零並修復(可能使用一些條件)。