我有以下标签云。
$rows = $db->loadObjectList();
foreach ($rows as $row)
{
$strAllTags .= $row->caption . ",";
}
// Store frequency of words in an array
$freqData = array();
// Get individual words and build a frequency table
foreach( explode(",", $strAllTags) as $word )
{
// For each word found in the frequency table, increment its value by one
array_key_exists( trim($word), $freqData ) ? $freqData[ trim($word) ]++ : $freqData[ trim($word) ] = 0;
}
function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 32 )
{
$minimumCount = min( array_values( $data ) );
$maximumCount = max( array_values( $data ) );
$spread = $maximumCount - $minimumCount;
$cloudHTML = '';
$cloudTags = array();
$spread = 55;
foreach( $data as $tag => $count )
{
if ($count > 4)
{
$size = $minFontSize + ( $count - $minimumCount )
* ( $maxFontSize - $minFontSize ) / $spread;
$cloudTags[] = '[[a style="font-size: ' . floor( $size ) . 'px'
. '" class="tag_cloud" href="/home?func=search&searchfield=' . $tag
. '"]]'
. htmlspecialchars( stripslashes( $tag ) ) . '[[/a]]';
}
}
return join( "\n", $cloudTags ) . "\n";
}
echo getCloud($freqData);
?>
它工作得很好,我只需要将它限制在前 20 个结果中,关于如何做到最好的任何想法?
谢谢,如果您需要查看其余代码,请告诉我。