간단한 태그 클라우드 구현

4245 단어 라벨
구현 코드:
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( "
", $cloudTags ) . "
"; }

 
호출 코드:
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);

}

좋은 웹페이지 즐겨찾기