features
This modification on Sphider, a PHP based site search engine will create a create a web2.0 style search / word / tag cloud (whatever you want to call it). I'ts not my own code, I have just modified it to work with sphider. Credit for the creator is in the code. Click here for an example of what it looks like, along with pure CSS3 rounded corners!
Add the follwoing code to a php page, cloud.php for example:
<style>
#tags {
float: left;
width: 220px;
margin: 10px 0px;
padding: 10px;
background:#ececec;
color:#333;
border: 1px dashed #cccccc;
text-align: justify;
}
#tags .title {
font-family:arial;
font-size:16px;
font-weight:bold;
background-color: transparent;
padding: 4px 4px;
color:#808080;
}
#tags a {
color:#39c;
background-color: transparent;
padding: 2px 4px;
white-space: nowrap;
text-decoration: none;
font-family:arial;
}
#tags a:hover {
color:#39c;
background: green;
text-decoration: none;
}
</style>
<div id="tags">
<div class="title">Popular Searches</div>
<?php
// Snippet taken from [prism-perfect.net]
include "/path/to/public_html/search/settings/database.php";
include "/path/to/public_html/search/settings/conf.php";
// In the SQL below, change these three things:
// thing is the column name that you are making a tag cloud for
// id is the primary key
// my_table is the name of the database table
// added by essexboyracer:
// dont return zero result words and limit the cloud to 10 phrases
$query = "SELECT query AS tag, COUNT(*) AS quantity
FROM sphider_query_log
WHERE results > 0
GROUP BY query
ORDER BY query ASC
LIMIT 10";
$result = mysql_query($query) or die(mysql_error());
// here we loop through the results and put them into a simple array:
// $tag['thing1'] = 12;
// $tag['thing2'] = 25;
// etc. so we can use all the nifty array functions
// to calculate the font-size of each tag
while ($row = mysql_fetch_array($result)) {
$tags[$row['tag']] = $row['quantity'];
}
// change these font sizes if you will
$max_size = 30; // max font size in %
$min_size = 11; // min font size in %
// get the 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 (0 == $spread) { // we don't want to divide by zero
$spread = 1;
}
// determine the font-size increment
// this is the increase per tag quantity (times used)
$step = ($max_size - $min_size)/($spread);
// loop through our tag array
foreach ($tags as $key => $value) {
// calculate CSS 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 = $min_size + (($value - $min_qty) * $step);
// uncomment if you want sizes in whole %:
// $size = ceil($size);
// you'll need to put the link destination in place of the #
// (assuming your tag links to some sort of details page)
echo '<a href="/search/search.php?query='.$key.'&search=1" style="font-size: '.$size.'px"';
// perhaps adjust this title attribute for the things that are tagged
echo ' title="'.$value.' things tagged with '.$key.'"';
echo '>'.$key.'</a> ';
// notice the space at the end of the link
}
?>
</div>