Welcome! Log In Create A New Profile

Advanced

[MOD] Search, Tag, Word Cloud

Posted by essexboyracer 
[MOD] Search, Tag, Word Cloud
August 24, 2007 05:30AM
Hi All

My first time here, thought I would give back this snippet for creating a web2.0 style search / word / tag cloud (whatever you want to call it). I'ts not my own code, credit for the creator is in the code.

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 http://prism-perfect.net/archive/php-tag-cloud-tutorial/

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);

// 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>



Edited 1 time(s). Last edit at 08/30/2007 06:35AM by essexboyracer.
Re: [MOD] Search, Tag, Word Cloud
September 05, 2007 10:52PM
A tag cloud could be verry verry cool to show the most search terms ! Thanks a lot. I'll try your mod as soon as I will finish my Sphider's installation !

ps : Almost all, keep your mod updated and compatible with futures Sphider upgrades please smiling smiley...
Re: [MOD] Search, Tag, Word Cloud
October 20, 2007 10:29AM
Could someone explain what this cloud tag means or does?
Re: [MOD] Search, Tag, Word Cloud
October 21, 2007 03:17AM
I believe is something like this:

http://forge.mysql.com/snippets/


under "Browse By Tag"

Diego Medina
[url=http://www.fmpwizard.com]Web Developer[/url]
Re: [MOD] Search, Tag, Word Cloud
October 26, 2007 06:43PM
Wonderful code. Absolutely wonderful. Thank you so much for posting it!
Re: [MOD] Search, Tag, Word Cloud
October 28, 2007 05:02PM
Absolutely wonderful. Thank you so much.
Re: [MOD] Search, Tag, Word Cloud
October 28, 2007 08:15PM
thank you!
Re: [MOD] Search, Tag, Word Cloud
October 29, 2007 09:31PM
you have a small error ...

please change this:

$result = mysql_query($query);

with this:

$result = mysql_query($query) or die(mysql_error());

and it will work just fine!
Re: [MOD] Search, Tag, Word Cloud
October 29, 2007 10:07PM
sorry my friand can you expline more how we make the mod work
spechel the mysql


// 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);

i dont understaind how i do make it work
and thanks
Re: [MOD] Search, Tag, Word Cloud
June 03, 2008 10:00PM
I have been using this code for about 3 weeks now... 180,000+ rows have already been amassed in my query_log, growing by 10,000+ a day, and this script is not optimized to handle a large amount of rows because it was taking 6 seconds or more to run on a Intel Xeon-Clovertown 5310-QuadCore 4 GB ram dedicated server. This script was the culprit behind my server load of up to 90 (it used to hover around 2)!

This script was great in the beginning for SEO as google seemed to love it ; as they already indexed over 10,000 pages of content because of this script. I am currently looking for a better alternative and I will post it here if I find one.

Just thought I would let everyone know my experience with this script.



Edited 1 time(s). Last edit at 06/03/2008 10:02PM by readyman.
Re: [MOD] Search, Tag, Word Cloud
June 05, 2008 03:37AM
Nice code!
Question: Is there a similar code available that can work with txt or php files, instead of mysql?
Re: [MOD] Search, Tag, Word Cloud
June 15, 2008 05:27AM
I dont see why not nick. at the end of the day it doesn't really matter where your words come from, so long as there is an array with a tag and a quantity. If your text file looked like this:

food|10
cars|26
travel|8

Then you could read in each line, split on the pipe then assign to the array. Have a play around. Have a look in the PHP manual at www.php.net for things like file, fopen, explode, string array.

Tring Web Design Services, Hertfordshire, UK
http://www.tring-web-design.co.uk
Re: [MOD] Search, Tag, Word Cloud
June 23, 2008 06:28PM
Hi Guys,

Im trying to ammend the script but it is causing me a hassle and not working!

Ive entered code:

// 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 db248038120.query_log
WHERE results > 0
GROUP BY query
ORDER BY query ASC
LIMIT 10";

$result = mysql_query($query) or die(mysql_error());


This brings up result on published php:

SELECT command denied to user 'dbo229293774'@'212.227.127.30' for table 'query_log'

Why is it pointing to my other mysql db.

How do I rectify pls HELP????????

Thks, AJ
Re: [MOD] Search, Tag, Word Cloud
July 19, 2008 03:53PM
ajaysangha, insert your table prefix, in settings/database.php

Examplesad smileyWith prefix search_)

$query = "SELECT query AS tag, COUNT(*) AS quantity
FROM db248038120.query_log
WHERE results > 0
GROUP BY query
ORDER BY query ASC
LIMIT 10";

Try

$query = "SELECT query AS tag, COUNT(*) AS quantity
FROM db248038120.search_query_log
WHERE results > 0
GROUP BY query
ORDER BY query ASC
LIMIT 10";


smiling smiley
Re: [MOD] Search, Tag, Word Cloud
November 20, 2008 05:24AM
how do you install this mod?
step by step please
Re: [MOD] Search, Tag, Word Cloud
December 11, 2008 10:35AM
Hi all,

modified the TS-post a bit, to take advantage of the predefined variables in Sphider.

Simply create a file called cloud.php and copy/paste the following:

Quote

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SearchCloud</title>
<style>
#tags {
float: none;
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: silver;
text-decoration: none;
}
</style>

</head>

<body>
<div id="tags">
<div class="title">Populaire Zoektermen</div>
<?php
$include_dir = "./include";
$template_dir = "./templates";
$settings_dir = "./settings";
$language_dir = "./languages";

require_once("$settings_dir/database.php"winking smiley;
require_once("$language_dir/en-language.php"winking smiley;
require_once("$include_dir/searchfuncs.php"winking smiley;
require_once("$include_dir/categoryfuncs.php"winking smiley;

include "$settings_dir/conf.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 ".$mysql_table_prefix."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.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>
</body>
</html>

Just upload the file to the main-map of Sphider.

Now edit the very bottom of search.php like so:

Original:
Quote

break;
}

include "$template_dir/$template/footer.html";
?>
Change to this:
Quote

break;
}
echo '<div align=center>';
include "cloud.php";
echo '</div>';
include "$template_dir/$template/footer.html";
?>

And Enjoy the SearchCloud !
Hope it helps anyone.

Grts



Edited 1 time(s). Last edit at 12/11/2008 10:58AM by OrBeX.
lex
Re: [MOD] Search, Tag, Word Cloud
June 04, 2009 03:26PM
thank you OrBeX your modified code works perfect
Re: [MOD] Search, Tag, Word Cloud
July 01, 2009 05:25PM
Excellet mod, thanks.

What if people start using bad language etc, can these keywords be removed?

Whats the best way to delete them?

Cheers
Re: [MOD] Search, Tag, Word Cloud
July 22, 2009 03:45AM
anything for wordpress
Re: [MOD] Search, Tag, Word Cloud
August 13, 2009 07:15PM
Hello you all,
sorry but i can not help, only give you an idea:

How if one could use sphider not only to spider a website but aditionally extract the keywords for every single page to add it to the meta tag?
It should be possible to make a textfile containing the urls of all spidered pages followed by the keywords.
What do you think about this idea?

Greetings
oldpete
jc
Re: [MOD] Search, Tag, Word Cloud
September 28, 2009 06:53PM
Any thoughts on how to mod this script so it only returns the top 25 search terms?
Re: [MOD] Search, Tag, Word Cloud
September 30, 2009 08:55PM
Modify the SQL query like this.

$query = "SELECT query AS tag, COUNT(*) AS quantity
FROM ".$mysql_table_prefix."query_log
WHERE results > 0
GROUP BY query
ORDER BY query ASC
LIMIT 25";

The limit set the limit of how many it returns
Re: [MOD] Search, Tag, Word Cloud
January 15, 2010 10:20AM
This is a great mod. Thanks for posting!

[url=http://www.bauerwebstudios.net]webdesign[/url] | [url=http://links.bauerwebstudios.net]website aanmelden[/url]
Re: [MOD] Search, Tag, Word Cloud
November 11, 2011 05:08PM
This is a very nice mod and have been using it for years.

When someone searches using multiple words, the url in the search results shows

/search.php?query=business+check+binders&search=1

which we like.

However, the cloud records the url not with plus signs, but with %20, ie:

/search.php?query=business%20check%20binders&search=1

So when a user clicks on the term: business check binders we lose the SEF URLs.

Any idea how to:

a) create a mod rewrite in .htacess to replace %20 with + signs, or
b) have the cloud show + signs instead of %20

?

Thanks!



Edited 1 time(s). Last edit at 11/11/2011 05:09PM by Convergence.
Re: [MOD] Search, Tag, Word Cloud
November 11, 2011 06:17PM
Figured it out! Not an .htaccess fix but a simple change to the cloud.php code:

Replace:

echo '<a href="./search.php?query='.$key.'&search=1" style="font-size: '.$size.'px"';



With

echo '<a href="./search.php?query='.urlencode($key).'&search=1" style="font-size: '.$size.'px"';



Edited 1 time(s). Last edit at 11/11/2011 06:23PM by Convergence.
Re: [MOD] Search, Tag, Word Cloud
November 24, 2011 08:37PM
Does anybody have another tag cloud? Have you tested this tag cloud? It works for some number of words but then when you search a word several times, it doesn't even show up sad smiley
Re: [MOD] Search, Tag, Word Cloud
November 28, 2011 06:07PM
test-sphider Wrote:
-------------------------------------------------------
> Does anybody have another tag cloud? Have you
> tested this tag cloud? It works for some number of
> words but then when you search a word several
> times, it doesn't even show up sad smiley

Been using it for years without any problems. Words only show up in the cloud if there are search results. If a word isn't indexed it won't show up.
Re: [MOD] Search, Tag, Word Cloud
November 29, 2011 01:37PM
Convergence Wrote:
-------------------------------------------------------
> test-sphider Wrote:
> --------------------------------------------------
> -----
> > Does anybody have another tag cloud? Have you
> > tested this tag cloud? It works for some number
> of
> > words but then when you search a word several
> > times, it doesn't even show up sad smiley
>
> Been using it for years without any problems.
> Words only show up in the cloud if there are
> search results. If a word isn't indexed it won't
> show up.


I guess the problem is with non-english characters. Whem I search for a word that belongs to latin5 charset, it doesn't show up..
Sorry, only registered users may post in this forum.

Click here to login