I would like to log the search terms that people are typing in when they come to my site. How can I log this information in my database?
David,
I went through the script and it does not seem to be setup to do exactly what I want.
Let me explain exactly what I am trying to do. I have created a script that will be used to display a "tag cloud" with the Most popular searches on my site so that a visitor will see what others are searching on. If a identical search term is used it will be incremented by one each time it is used. Here is the script.
CREATE TABLE tags (
id int(11) NOT NULL,
tag varchar(100) NOT NULL,
count int(11) NOT NULL DEFAULT '0'
);
Using whatever method you use, create the table in your database and then we will continue.
Next we need to modify the function "get_tag_data()" that read in the array in the previous tutorial.
function get_tag_data() {
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$result = mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC");
while($row = mysql_fetch_array($result)) {
$arr[$row['tag']] = $row['count'];
}
ksort($arr);
return $arr;
}
I can manipulate the Tag cloud by manually changing the hit counter for each search term in the database, but I would like to have this automatically generated by people typing in search terms.
Thanks for your help.
Hi,
This should be a straight forward change to the code that you have added to search.php to log the "tags". Can you post the code that you are using to do this, and i'll work out the changes for you to make it increment your count value...
Cheers,
David.
I have created a tagcloud.php and placed it in the Html folder. This function gets called when someone visits my home page and displays the search terms.
<?php
/*
Example usage of the Tag Cloud Script (static array)
*/
function get_tag_data() {
mysql_connect('Localhost', 'DBusername', 'DBpassword'); mysql_select_db('Localhost'); $result = mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC");
while($row = mysql_fetch_array($result)) {
$arr[$row['tag']] = $row['count'];
}
ksort($arr);
return $arr;
}
function get_tag_cloud() {
// Default font sizes
$min_font_size = 12;
$max_font_size = 30;
// Pull in tag data
$tags = get_tag_data();
$minimum_count = min(array_values($tags));
$maximum_count = max(array_values($tags));
$spread = $maximum_count - $minimum_count;
if($spread == 0) {
$spread = 1;
}
$cloud_html = '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $count) {
$size = $min_font_size + ($count - $minimum_count)
* ($max_font_size - $min_font_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px'
. '" class="tag_cloud" href="http://www.therealtrader.com/estores/search?q=' . $tag
. '" title="\'' . $tag . '\' returned a count of ' . $count . '">'
. htmlspecialchars(stripslashes($tag)) . '</a>';}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;
}
?>
<style type="text/css">
.tag_cloud { padding: 3px; text-decoration: none; }
.tag_cloud:link { color: #81d601; }
.tag_cloud:visited { color: #019c05; }
.tag_cloud:hover { color: #ffffff; background: #69da03; }
.tag_cloud:active { color: #ffffff; background: #ACFC65; }
</style>
<h3>See what others are searching for!</h3>
<div id="wrapper">
<!-- BEGIN Tag Cloud -->
<?php
// Display the Tag Cloud in an HTML format
print get_tag_cloud();
?>
<!-- END Tag Cloud -->
</div>
Hi,
What I actually need to see is your modifications to search.php - where you actually insert the query (tag in this instance) into the database - then I can show you how to update that to increment the count each time...
Cheers,
David.
David,
I have not modified the search.php in any way. my tagcloud.php script is not linked to the search.php and that is where I need your help.
If you look at about lie 33 in the script you will see this line.
. '" class="tag_cloud" href="http://www.therealtrader.com/estores/search?q=' . $tag
This enters a tag cloud search term when someone clicks on that term and a new search is performed. I did not modify the search.php to accomplish this.
Ah, I understand now!
Ok, firstly, in html/searchform.php we need to add a hidden field to tell the search script to log the query. This is because you don't want all the other system based calls to search.php to be logged. Change the search form as follows:
<div class='searchform'>
<form name='search' action='<?php print $config_baseHREF ?>search.php'>
<input type='text' name='q' size='35' value='<?php print (isset($q)?$q:""); ?>' />
<input type='hidden' name='log' value='1' />
<input type='submit' value='<?php print translate("Search"); ?>' />
</form>
</div>
If you have already modified your search form for other reasons, just add that new line containing the hidden form field "log".
Next, in search.php you need write the query into your tag database if log=1 in the URL. Starting at line 13 you will see the following code:
if ($q)
{
.. rest of code ..
Add the code below to this to write the query into the database, so you will have the following:
if ($q)
{
if ($_GET["log"])
{
$sql = "SELECT * FROM tags WHERE tag='".database_safe($q)."'";
if (database_querySelect($sql,$rows))
{
$sql = "UPDATE tags SET count=count+1 WHERE tag='".database_safe($q)."'";
}
else
{
$sql = "INSERT INTO tags SET tag='".database_safe($q)."',count='1'";
}
database_queryModify($sql,$result);
}
.. rest of code ..
The only problem you may have is that I notice from your table definition that you have not declared ID as an autoincrement field, so you would need to update that before inserting records automatically.
Hope this helps!
Cheers,
David.
Hi,
There's a step-by-step guide (read through the entire thread before you start) to doing this on the following page:
http://www.pricetapestry.com/node/822
Cheers,
David.