Yesterday day we built a pretty cool tag cloud script in php/mysql that gets the referring search keyword and inputs it into a database… and from there it can be pulled for the tag cloud information. We thought this would be useful information for everyone so we’ll share how we did it.
1. Create your database… we created fields: number, term, cityurl, countyurl, abbr IN table tag_cloud
2. In the head of your php page place the following:
**note: this is based on a state, county, city setup. Your code will most likely vary.
-
<?php
-
-
$abbr = $_GET['state'];
-
$countyurl = $_GET['countyurl'];
-
$cityurl = $_GET['cityurl'];
-
-
$parse = parse_url($_SERVER['HTTP_REFERER']);
-
$se = $parse["host"];
-
$raw_var = explode("&", $parse["query"] );
-
foreach ($raw_var as $one_var) {
-
$raw = explode("=", $one_var);
-
$var[$raw[0]] = urldecode ($raw[1]);
-
}
-
$se = explode (".", $se);
-
switch ($se[1]) {
-
case 'yahoo':
-
$keywords = $var['p'];
-
break;
-
case 'aol':
-
$keywords = $var['query'];
-
break;
-
default:
-
$keywords = $var['q'];
-
}
-
unset($parse, $se, $raw_var, $one_var, $var);
-
-
include 'config.php'; //you need to connect to the db
-
include 'opendb.php';
-
-
if($keywords != NULL) { //this says if there is a keyword enter it to the database
-
mysql_query ("INSERT INTO tag_cloud (term, countyurl, cityurl, abbr) VALUES ('$keywords', '$countyurl', '$cityurl', '$abbr')");
-
}
-
-
include 'closedb.php';
-
?>
Now where ever on your page that you want to display the tag cloud enter the following code:
-
<?php
-
include 'config.php';
-
include 'opendb.php'; // connect to database
-
-
$query = "SELECT term AS tag, COUNT(term) AS quantity
-
FROM tag_cloud
-
WHERE abbr='$abbr' AND cityurl='$cityurl'
-
GROUP BY term
-
ORDER BY term ASC"; //this pulls the tag cloud information from the DB
-
-
$result = mysql_query($query);
-
-
// here we loop through the results and put them into a simple array:
-
// 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 = 200; // max font size in %
-
$min_size = 100; // 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="/city/' .$abbr. '-' .$countyurl. '-' .$cityurl. '.html" style="font-size: '.$size.'%"';
-
// 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
-
}
-
-
include 'closedb.php';
-
-
?>
You can view this working here: Grand Forks Real Estate
We’d like to thank Shawn at digitalpoint forums & Prism-Perfect for snippets of code.
Posted under Web Design
This post was written by Coyol on September 11, 2008
