You are here:  » Popular Searches in Sidebar


Popular Searches in Sidebar

Submitted by mikecdprice on Sun, 2012-07-22 04:15 in

Hello,

Is it possible to add popular searches to the sidebar. What I would like is a mod to show popular searches for keyword searched if any found. If none found then show the top searches so it is not blank.

For example keyword searched:

Gucci

in the search results it would show in sidebar:

Popular Searches:

Gucci Envy
Gucci Purses
Gucci Mice ( :)

etc

Is this possible?

Thank you

Submitted by support on Sun, 2012-07-22 10:33

Hello Mike,

Sure - this is very similar to the query log with count modification (http://www.pricetapestry.com/node/892) but I would suggest moving the log code to a point at which search results have been found so that queries with no results are not logged. First create your querylog table with a dbmod.php script as follows:

<?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.";
?>

With the table created, modify search.php as follows by looking for the following code at line 279:

    if ($resultCount)
    {

...and REPLACE with:

    if ($resultCount)
    {
      if ($_GET["log"])
      {
        $sql = "INSERT IGNORE 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);
      }

With that in place, your popular searches code, which you can place wherever required first looking for queries containing the current keyword(s); or if not then top searches overall is as follows;

  if (strpos($_SERVER["PHP_SELF"],"/search.php")!==FALSE && $q)
  {
    $limit = 5;
    $rows = array();
    $sql = "SELECT * FROM `".$config_databaseTablePrefix."querylog` WHERE query LIKE '%".database_safe($q)."%' ORDER BY count DESC LIMIT ".$limit;
    database_querySelect($sql,$rows);
    if (!count($rows))
    {
      $sql = "SELECT * FROM `".$config_databaseTablePrefix."querylog` ORDER BY count DESC LIMIT ".$limit;
      database_querySelect($sql,$rows);
    }
    if (count($rows))
    {
      print "<p>Popular Searches</p>";
      print "<ul>";
      foreach($rows as $row)
      {
        $url = $config_baseHREF."search.php?q=".urlencode($row["query"]);
        print "<li><a href='".$url."'>".$row["query"]."</a></li>";
      }
      print "</ul>";
    }
  }

Change the value of $limit for more or fewer results as required...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by fstore on Sun, 2012-07-22 13:32

HI David
I was also thinking of the similar feature for my site.

The solution you described above, is that for main PT site and not for the PT wordpress plugin site?

How can we achieve similar for PT wordpress plugin site? is there any way to use wordpress tag cloud widget?

Regards
Hassan

Submitted by support on Mon, 2012-07-23 11:09

Hello Hassan,

It would be straight forward to implement a popular searches widget. Use the same dbmod.php script to create the querylog table in the Price Tapestry database, then to log queries look for the following code at line 331 of pto_search.php:

    $html .= pto_search_html();

...and REPLACE with:

    $html .= pto_search_html();
    if (!isset($parts[1]) || $parts[1]=="")
    {
      $sql = "INSERT INTO `".$pto_config_databaseTablePrefix."querylog` SET query='".$wpdb->escape($pto_q)."'";
      $wpdb->query($sql);
      $sql = "UPDATE `".$pto_config_databaseTablePrefix."querylog` SET count=count+1 WHERE query='".$wpdb->escape($pto_q)."'";
      $wpdb->query($sql);
    }

And then to create the Popular Searches widget; add the following object code to the end of pto_search.php

class pto_search_popular_widget extends WP_Widget {
      function pto_search_popular_widget()
      {
        parent::WP_Widget(false, $name = "PriceTapestry.org Popular Searches");
      }
      function widget($args, $instance)
      {
        global $pto_config_permalink;
        global $pto_config_databaseTablePrefix;
        global $pto_q;
        global $wpdb;
        if (!isset($pto_q)) return;
        extract($args);
        $title = apply_filters("widget_title", $instance["title"]);
        print $before_widget;
        if ($title) print $before_title.$title.$after_title;
        $html = "";
        $limit = 5;
        $rows = array();
        $sql = "SELECT * FROM `".$pto_config_databaseTablePrefix."querylog` WHERE query LIKE '%".$wpdb->escape($q)."%' ORDER BY count DESC LIMIT ".$limit;
        $wpdb->query($sql);
        if (!count($wpdb->last_result))
        {
          $sql = "SELECT * FROM `".$pto_config_databaseTablePrefix."querylog` ORDER BY count DESC LIMIT ".$limit;
          $wpdb->query($sql);
        }
        if (count($wpdb->last_result))
        {
          $html .= "<ul>";
          foreach($wpdb->last_result as $row)
          {
            $url = get_bloginfo('url').$pto_config_permalink."?pto_q=".urlencode($row->query);
            $html .= "<li><a href='".$url."'>".$row->query."</a></li>";
          }
          $html .= "</ul>";
        }
        print $html;
        print $after_widget;
      }
      function update($new_instance, $old_instance)
      {
        $instance = $old_instance;
        $instance["title"] = strip_tags($new_instance["title"]);
        return $instance;
      }
      function form($instance)
      {
        $title = esc_attr($instance["title"]);
        print "<p><label for='".$this->get_field_id('title')."'>"._e("Title:")." <input class='widefat' id='".$this->get_field_id('title')."' name='".$this->get_field_name('title')."' type='text' value='".htmlentities($title,ENT_QUOTES,get_settings("blog_charset"))."' /></label></p>";
      }
}

Finally, to register the widget; look for the following code at line 116 of pto.php:

  register_widget('pto_atoz_widget');

...and REPLACE with:

  register_widget('pto_atoz_widget');
  register_widget('pto_search_popular_widget');

Cheers,
David.
--
PriceTapestry.com

Submitted by fstore on Mon, 2012-07-23 12:19

Hi David
The new table ( pt_querylog ) is not getting populated...hence nothing is showing up on widget

please help..

thanks

Submitted by support on Mon, 2012-07-23 13:21

Sorry Hassan - I'd missed the database table prefix out of the log code - corrected above - the correct modification to pto_search.php is:

    $html .= pto_search_html();
    if (!isset($parts[1]) || $parts[1]=="")
    {
      $sql = "INSERT INTO `".$pto_config_databaseTablePrefix."querylog` SET query='".$wpdb->escape($pto_q)."'";
      $wpdb->query($sql);
      $sql = "UPDATE `".$pto_config_databaseTablePrefix."querylog` SET count=count+1 WHERE query='".$wpdb->escape($pto_q)."'";
      $wpdb->query($sql);
    }

Cheers,
David.
--
PriceTapestry.com

Submitted by fstore on Mon, 2012-07-23 13:36

Thanks David
I applied the changes, now table is getting populated but nothing is showing up in sidebar.
there are around 11 queries in the table but these are not being displayed.

Regards
Hassan

Submitted by support on Mon, 2012-07-23 15:02

Hello Hassan,

The database table prefix variable was incorrect in the widget code - replace $config_databaseTablePrefix with $pto_config_databaseTablePrefix - corrected in the above - that should be all it is...

Cheers,
David.
--
PriceTapestry.com

Submitted by fstore on Tue, 2012-07-24 11:49

It worked, Thanks David for your time.

I really appreciate it.

Regards
Hassan

Submitted by paddyman on Tue, 2013-10-29 11:22

Hi David,

Just trying to implement mod above in wordpress and database table isn't being populated. Does the code above still apply with current distribution ?

Thanks

Adrian

Submitted by support on Tue, 2013-10-29 11:38

Hi Adrian,

The SQL generation and display code were separated in the latest version so $pto_q and $parts aren't in scope - however I would have expected it to create a single empty record which may have been the case. For the latest distribution, have a go with the following REPLACEment:

    $html .= pto_search_html();
    global $pto_q;
    $parts = explode(":",$pto_q);
    if (!isset($parts[1]) || $parts[1]=="")
    {
      $sql = "INSERT INTO `".$pto_config_databaseTablePrefix."querylog` SET query='".$wpdb->escape($pto_q)."'";
      $wpdb->query($sql);
      $sql = "UPDATE `".$pto_config_databaseTablePrefix."querylog` SET count=count+1 WHERE query='".$wpdb->escape($pto_q)."'";
      $wpdb->query($sql);
    }

(it's just lines 2/3 added, the rest is identical)

Cheers,
David.
--
PriceTapestry.com

Submitted by paddyman on Tue, 2013-10-29 12:36

Hi David,

Excellent, working great now. There was indeed a single empty record in the database table!

Fantastic mod, and a great analytical tool to have :)

Cheers

Adrian

Submitted by gunawin on Fri, 2015-04-03 16:46

Hi David,

I tried to implement this in PT and the table pt_querylog is not populated.
Does the code above still apply with the latest distribution 15/01A?
and on which file normally to add the following code:
if (strpos($_SERVER["PHP_SELF"],"/search.php")!==FALSE && $q)
{
$limit = 5;
$rows = array();
............................

Please help

Thanks in advance,
Gunawin

Submitted by support on Fri, 2015-04-03 17:14

Hello Gunawin,

The code immediately above is for PriceTapestry.org for WordPress, but the code in the first reply here is current, so if that's what you've implemented but you're not seeing queries logged - don't forget to add the hidden form field log to your search form (html/searchform.php in the distribution).

<input type='hidden' name='log' value='1' />

If that's all in place, to check if it's a database issue you can enable database debug mode by changing line 6 in config.advanced.php as follows;

  $config_databaseDebugMode = TRUE;

...then make a query that should be logged again and any MySQL error will be displayed in full. If you're not sure from what is displayed where the problem lies copy the text of the error message and I'll check it out for you; otherwise if debug mode doesn't reveal anything, if you would like to email me your modified search.php and html/searchform.php I'll check them out in context for you...

Cheers,
David.
--
PriceTapestry.com