You are here:  » Top Searches

Support Forum



Top Searches

Submitted by niravdave on Mon, 2008-08-18 21:25 in

I was referring to this thread http://www.pricetapestry.com/node/892 and this thread seems to be pretty old and very confusing for a starter to implement top searched. Can it be a step by step guide with the newest version of PT.

Also i would like to know if there is a possibilty to implement predictive search?

And the last Q on how do i modify some words like 'visit store' to buy now.

thanks
Nirav

Submitted by support on Tue, 2008-08-19 09:27

Hello Nirav,

The bulk of the code in the post you refer to is still valid under the current version, but to bring the instructions together for you; firstly you need to create the new table "pt_querylog" in the database (assuming the default $config_databaseTablePrefix of "pt_"). The SQL to create the table is:

CREATE TABLE `pt_querylog` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`count` INT ( 11 ) NOT NULL ,
`query` VARCHAR( 255 ) NOT NULL ,
UNIQUE (
`query`
)
) ENGINE = MYISAM ;

You can either use a MySQL administration tool like phpMyAdmin to do this, or alternatively run the following PHP script in your main Price Tapestry folder, and then delete it:

maketable.php:

<?php
  
require("includes/common.php");
  
$sql =
"
CREATE TABLE `"
.$config_databaseTablePrefix."querylog` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`count` INT ( 11 ) NOT NULL ,
`query` VARCHAR( 255 ) NOT NULL ,
UNIQUE (
`query`
)
) ENGINE = MYISAM ;
"
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Having done this, the next step is to add the "log" parameter to the search form in order to tell the search script to log the query. To do this, simply change html/searchform.php to:

<div class='searchform'>
  <form name='search' action='<?php print $config_baseHREF ?>search.php'>
    <input type='text' name='q' id='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>

Then, to make search.php log the query, look for the following code at line 14:

  if ($q)
  {

...and add this new section immediate afterwards, inside the IF structure:

    if ($_GET["log"])
    {
      $sql = "INSERT INTO `".$config_databaseTablePrefix."querylog` SET query='".database_safe($q)."'";
      database_queryModify($sql,$result);
      $sql = "UPDATE `".$config_databaseTablePrefix."querylog` SET count=count+1 WHERE query='".database_safe($q)."'";
      database_queryModify($sql,$result);
    }

That's it for the main code changes. The PHP to display top (10) searches is then:

<?php
  
print "<p>Top Searches</p>";
  print 
"<ul>";
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."querylog` ORDER BY count DESC LIMIT 10";
  if (
database_querySelect($sql,$rows))
  {
    foreach(
$rows as $row)
    {
      
$url $config_baseHREF."search.php?q=".urlencode($row["query"]);
      print 
"<li><a href='".$url."'>".$row["query"]."</a></li>";
    }
  }
  print 
"</ul>";
?>

Regarding your other question; to change "Visit Store" to "Buy Now", simply search for the "Visit Store" text on line 14 of html/prices.php and change the text as required.

Hope this helps!

Cheers,
David.

Submitted by kempo on Wed, 2008-08-20 14:54

I also want to implement this option but I have a question before implement:
Does this function lists whatever has been searched only or whatever has been searched and returned search results?
Thanks.

Submitted by support on Wed, 2008-08-20 15:19

Hi Kempo,

As it stands based on the modifications above, this would store all queries whether or not they returned any results.

However, it would be easy to modify it to only store queries that returned results. The above instructions describe the place to insert the logging code into search.php at this point (line 14):

  if ($q)
  {

Alternatively, insert the logging code after the following code, which you will find around about line 120:

    if ($resultCount)
    {

Cheers,
David.

Submitted by kempo on Thu, 2008-08-21 07:21

Thanks David,
I asked that because someone can make a lot of search with spam terms like {content saved} and I don't want to show their spam queries in the top search list.
Cheers,

Submitted by atman on Tue, 2008-10-14 02:19

hi david,

i have a url structure like

example.com/search/keywords/

is it possible to log the traffic of those URLs and count it also as top searches?

thanks and best regards,

atman

Submitted by support on Tue, 2008-10-14 07:53

Hi atman,

Sure - all you would need to do is add the "log=1" parameter to the re-written URL in .htaccess.

To do this, look in your /search/ ReWrite rule for where you will already have:

rewrite=1

...and REPLACE this with:

rewrite=1&log=1

...leaving everything either side of this point unchanged. That should do the trick!

Cheers,
David.

Submitted by Hugo on Tue, 2008-10-28 12:36

Hi David,

First, as this is my firt post, let me say thank you for your nice script and your continual support.

I have succeed including this mod to my site, but whenever I insert the logging code here:

    if ($resultCount)
    {
            if ($_GET["log"])
{
$sql = "INSERT INTO querylog SET query='".database_safe($q)."'";
database_queryModify($sql,$result);
$sql = "UPDATE querylog SET count=count+1 WHERE query='".database_safe($q)."'";
database_queryModify($sql,$result);
}
      $resultFrom = ($offset+1);

The search results are not displayed and I've got this errors:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/****/www/includes/database.php on line 21
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/****/www/includes/database.php on line 26
Warning: Cannot modify header information - headers already sent by (output started at /home/****/www/includes/database.php:21) in /home/****/www/html/header.php on line 7

I've seached the forum, but did not find any answer to this error.

Do you have any clue on how to fix this, please?

Thanks,
Hugo

Submitted by support on Tue, 2008-10-28 17:36

Hi Hugo,

Thank you for your comments.

It sounds like the database modifications needed for this mod haven't been made correctly. I have emailed you the debug version of includes/database.php that will display the MySQL error message if any query fails, and this should describe exactly what is wrong...

Cheers,
David.

Submitted by don_load on Wed, 2010-07-28 20:05

hi, applied this mod and it all seems ok apart from a couple of small issues.

1. I'm getting this displayed on duplicate entrys with error reporting off..

[INSERT INTO querylog SET query='test'][Duplicate entry 'test' for key 'query']

2. When I move the code down to below 'if($results)' it errors. Judging by the error report its now trying to update/insert with the 'database_querySelect' function rather than the defined 'database_queryModify'. Not sure why its doing this as all I've done is cut and paste?

regards,
Jay

Submitted by support on Wed, 2010-07-28 20:09

Hi Jay,

That sounds like database debug code is hard coded into the database_queryModify() function in includes/database.php. Check for this code:

    if (!$result)
    {
      print "[".$sql."][".mysql_error()."]";
    }

...and either comment out or delete that block. If you're not sure; if you could email me your includes/database.php I'll check it out...

Cheers,
David.

Submitted by don_load on Sat, 2010-07-31 12:18

that pointed me in the right direction, thanks. Turned off error reporting but not $config_databaseDebugMode.

regards,
Jay

Submitted by don_load on Sat, 2010-07-31 12:48

On the 2nd issue I had regarding the errors when moving it in to the if($resultCount) statement. Had a close look at the debug_print_backtrace() on it and found an sql conflict. It seemed to be re-defining an already defined $sql and then later being used in the script to process the results. Changing to this solved it...

$logSQL = "INSERT INTO querylog SET query='".database_safe($q)."'";
database_queryModify($logSQL,$result);
$logSQL = "UPDATE querylog SET count=count+1 WHERE query='".database_safe($q)."'";
database_queryModify($logSQL,$result);

regards,
Jay