PHP Tag Cloud
13
Creates one of those neat Web 2.0-like tag clouds!
function printTagCloud($tags) {
// $tags is the array
arsort($tags);
$max_size = 32; // max font size in pixels
$min_size = 12; // min font size in pixels
// largest and smallest array values
$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));
// find the range of values
$spread = $max_qty - $min_qty;
if ($spread == 0) { // we don't want to divide by zero
$spread = 1;
}
// set the font-size increment
$step = ($max_size - $min_size) / ($spread);
// loop through the tag array
foreach ($tags as $key => $value) {
// calculate font-size
// find the $value in excess of $min_qty
// multiply by the font-size increment ($size)
// and add the $min_size set above
$size = round($min_size + (($value - $min_qty) * $step));
echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
}
}
$tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47, 'windows' => 82, 'apple' => 27);
printTagCloud($tags);
// $tags is the array
arsort($tags);
$max_size = 32; // max font size in pixels
$min_size = 12; // min font size in pixels
// largest and smallest array values
$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));
// find the range of values
$spread = $max_qty - $min_qty;
if ($spread == 0) { // we don't want to divide by zero
$spread = 1;
}
// set the font-size increment
$step = ($max_size - $min_size) / ($spread);
// loop through the tag array
foreach ($tags as $key => $value) {
// calculate font-size
// find the $value in excess of $min_qty
// multiply by the font-size increment ($size)
// and add the $min_size set above
$size = round($min_size + (($value - $min_qty) * $step));
echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
}
}
$tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47, 'windows' => 82, 'apple' => 27);
printTagCloud($tags);






Use: $password = makeRandomPassword($Length);
If $Length is not specified (ie $password = makeRandomPassword();) then it will default to 8 characters
TABLE tags {
id INT(9) NOT NULL,
content INT(9) NOT NULL,
type INT(2) NOT NULL
}
id: the id of the tagged item
content: the tag
type: the type of item tagged
you might try something like this:
while ($row = mysql_fetch_array(mysql_query($sql))) {
$tags[$row['content']]++;
}
I broke my head all day and couldn't figure out a way to do it. Anyway, I would like to know how I could get data from a MySQL database. The connection to the database has already been made, and the name of the table holding information about the searches (I am using Cloud Tags for recent search entries) is "search".
I have pasted below the structure of the MySQL table.
The field "id" holds the search number.
The field "query" holds the search itself (search terms/keywords).
The field "searches" holds the number (number of times a search keyword has been used).
Please let me know how I could do it.
DROP TABLE IF EXISTS `search`;
CREATE TABLE IF NOT EXISTS `search` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`query` varchar(100) collate latin1_general_ci NOT NULL DEFAULT '',
`searches` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=30 ;
great script! how would i customize the script so it shows the tags randomly. Not from Large to Small? Thanks!
You can email me at eolmos@academiclearningcompany.com
thanks again!!!
Steven . . .
function printTagCloud($tags) {
// $tags is the array
arsort($tags);
$max_size = 32; // max font size in pixels
$min_size = 12; // min font size in pixels
// largest and smallest array values
$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));
// find the range of values
$spread = $max_qty - $min_qty;
if ($spread == 0) { // we don't want to divide by zero
$spread = 1;
}
// set the font-size increment
$step = ($max_size - $min_size) / ($spread);
// loop through the tag array
foreach ($tags as $key => $value) {
// calculate font-size
// find the $value in excess of $min_qty
// multiply by the font-size increment ($size)
// and add the $min_size set above
$size = round($min_size + (($value - $min_qty) * $step));
echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
}
}
$tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47, 'windows' => 82, 'apple' => 27);
printTagCloud($tags);
---------------------------------------------------------------------------
XXX stream
great script! how would i customize the script so it shows the tags randomly. Not from Large to Small?
thanks again!!!
Steven . . .
function printTagCloud($tags) {
// $tags is the array
arsort($tags);
$max_size = 32; // max font size in pixels
$min_size = 12; // min font size in pixels
// largest and smallest array values
$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));
// find the range of values
$spread = $max_qty - $min_qty;
if ($spread == 0) { // we don't want to divide by zero
$spread = 1;
}
// set the font-size increment
$step = ($max_size - $min_size) / ($spread);
// loop through the tag array
foreach ($tags as $key => $value) {
// calculate font-size
// find the $value in excess of $min_qty
// multiply by the font-size increment ($size)
// and add the $min_size set above
$size = round($min_size + (($value - $min_qty) * $step));
echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
}
}
$tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47, 'windows' => 82, 'apple' => 27);
printTagCloud($tags);
Code below
$cloud[] = '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
}
shuffle($cloud);
for($x=0;$x<count($cloud);$x++)
{
echo $cloud[$x];
}