实现代码:
function _cloudHtml($data = array(), $minFontSize = 12, $maxFontSize = 30) { $minimumCount = min(array_values($data)); $maximumCount = max(array_values($data)); $spread = $maximumCount - $minimumCount; $cloudHTML = ''; $cloudTags = array(); $spread == 0 && $spread = 1; foreach ($data as $tag => $count) { $size = $minFontSize + ($count - $minimumCount) * ($maxFontSize - $minFontSize) / $spread; $cloudTags[] = '<a style="font-size: ' . floor( $size ) . 'px"' . ' href="tag.php?name=' . rawurlencode($tag) . '"' . ' title="' . $tag . ' (' . $count . ')">' . htmlspecialchars(stripslashes( $tag)) . '</a>'; } return join( "\n", $cloudTags ) . "\n"; }
调用代码:
function _tagCloud() { $sql = "select tag, count(1) as count from tbs_tags group by tag"; $rows = $this->db->getAll($sql); foreach ($rows as $row) { $tags[$row['tag']] = $row['count']; } return $this->_cloudHtml($tags, 12, 24); }