Tag Cloud Creation in PHP
We have created a simple Tag Cloud feature in our core php programming. Basically the Tag Clod is
available in word press or other content management system. Tag Cloud is the type of feature that
indicates the hitting on the particular tag or topic. In word press the blog comes under particular tag. If
people visit the blog repeatedly then hit count increase then the particular tag name displays larger.
We used this concept to implement tag cloud in our site. In our site we wanted to show the category where
Non-Profit organization are working more. This means that when people select the category more number of times, it will be displayed larger.
Here are the steps followed :
1.Get the category names from the table.
2.Count the number of times the category name is repeated.
3.Generate CSS for tag cloud.
4.Display it on the page
//database code
function tag_cloud_data()
$orgArr=”";
$query=”your query”;
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$orgArr.=$row[0].” “;
}
return $orgArr;
}
// ==============================================================
// Function to actually generate the cloud from provided data =
// ==============================================================
function getCloud($data = array(), $minFontSize = 12, $maxFontSize = 24 )
{
$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 )
{
/*###### Code to generate Category id of the category ####*/
$query=”SELECT your tag name FROM table WHERE (condition)”;
$result mysql_query($query);
while($row=mysql_fetch_row($result)){
$orgCatId=$row[0];
$dataOrgCatId = $orgCatId;
/*###### Code to generate Category id of the category ####*/
$size = $minFontSize + ( $count – $minimumCount )
* ( $maxFontSize – $minFontSize ) / $spread;
$cloudTags[] = ‘‘
. htmlspecialchars( stripslashes( $tag ) ) . ‘‘;
}
return join( “\n”, $cloudTags ) . “\n”;
}
In html part you have to write
$orgArr=$oCall->tag_cloud_data();
$iNgocatId=explode(‘,’,$orgArr);
// Store frequency of words in an array
$freqData = array();
// Random words
foreach($iNgocatId as $value){
if($value!=0){
$lorem.=$oCall->get_ngocatname($value).”,”;
}
}
// Get individual words and build a frequency table
foreach( str_word_count( $lorem, 1 ) as $word )
{
// For each word found in the frequency table, increment its value by one
array_key_exists( $word, $freqData ) ? $freqData[ $word ]++ : $freqData[ $word ] = 0;
}
In this code the function name tag_cloud_data() will get the value from the database .The next
function getCloud() will create cloud text .The last code will generate html format tag which will display
on your page.
Latest posts by Koushik Nandy
- Pagination based on Arrays in PHP - August 6th, 2010
- Websites and their coding security - July 26th, 2010
- Basic PHP Security Checklist - July 16th, 2010
- PHP5 time zone solution - July 9th, 2010
- Converting XML to HTML - July 8th, 2010
