You are here:  » Adding Recent Searches


Adding Recent Searches

Submitted by twdesigns on Mon, 2011-12-19 15:36 in

Before I had it set up so that recent searches would be displayed. I assume I would need to modify my PT installation like before but after that how can I display recent searches in a WP widget or Template insertion?

Thanks!

Submitted by support on Mon, 2011-12-19 15:52

Hi,

That's correct - if you are using the plugin against a clean Price Tapestry installation then first of all make the same modifications as used previously so that the logging table is added to the database. If you could let me know when that's in place; and confirm the table name that you are using e.g. "querylog" then I'll work out the code for logging within the plugin and a recent searches widget for you...

Cheers,
David.
--
PriceTapestry.com

Submitted by twdesigns on Mon, 2011-12-19 17:03

I have installed everything per the previous instructions and my table is pt_querylog.

Submitted by support on Tue, 2011-12-20 10:18

Hi Tommy,

Edit: Updated for PriceTapestry.org for WordPress 2.0

OK, first in the Search Form HTML (wp-admin > Settings > PriceTapestry.org), just before the closing </form> tag, add

<input type='hidden' name='pto_log' value='1'>

Changes in pto.php

Look for the following code at line 111:

  register_widget('pto_search_filters_widget');

...and REPLACE with:

  register_widget('pto_search_filters_widget');
  register_widget('pto_search_top10_widget');

Look for the following code at line 563:

  array_push($vars, 'pto_q');

...and REPLACE with:

  array_push($vars, 'pto_q');
  array_push($vars, 'pto_log');

Changes in pto_search.php

Look for the following code at line 390:

    $html .= pto_search_html();

...and REPLACE with:

    $html .= pto_search_html();
    global $pto_log;
    if ($pto_log)
    {
      $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 finally add the following code to the end of the file, immediately before the closing PHP tag - this is the actual Top 10 widget code:

class pto_search_top10_widget extends WP_Widget {
  function pto_search_top10_widget()
  {
    parent::WP_Widget(false, $name = "PriceTapestry.org Search Top 10");
  }
  function widget($args, $instance)
  {
    global $pto_config_permalink;
    global $pto_config_databaseTablePrefix;
    global $wpdb;
    extract($args);
    $title = apply_filters("widget_title", $instance["title"]);
    print $before_widget;
    if ($title) print $before_title.$title.$after_title;
    $html = "";
    $sql = "SELECT * FROM `".$pto_config_databaseTablePrefix."querylog` ORDER BY count DESC LIMIT 10";
    if ($numRows = $wpdb->query($sql))
    {
      $html .= "<ul>";
      for($i=0;$i<$numRows;$i++)
      {
        $querylog = $wpdb->last_result[$i];
        $html .= "<li><a href='".get_bloginfo('url').$pto_config_permalink."?pto_q=".urlencode($querylog->query)."'>".$querylog->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"]);
    $instance["section"] = strip_tags($new_instance["section"]);
    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>";
  }
}

You mentioned recent searches in your original post - I've recreated the above from the Top 10 searches code from the standalone Price Tapestry forum. Recent searches can be added easily by adding a timestamp field of type INT(11) to the querylog table and then updating this at log time (with the return value of PHP's time() function) as well as the count field; and a similar widget created using ORDER BY timestamp DESC instead of ORDER BY count DESC - let me know of course if you're not sure...

Cheers,
David.
--
PriceTapestry.com

Submitted by twdesigns on Tue, 2011-12-20 15:46

David,

For some reason it's not displaying the searches. I checked to verify that data is being saved to pt_querylog and it is logging fine. I added all the above code and then loaded the widget but nothing displays. Any idea if I've overlooked something or what my problem might be. I'm going to go back over everything again to make sure.

Submitted by support on Tue, 2011-12-20 16:47

I don't see anything obviously wrong - i'll recreate on my test server and check it out for you - i'll drop you an email to let you know as I'll be offline shortly this evening but will give it a go first thing...

Cheers,
David.
--
PriceTapestry.com

Submitted by support on Wed, 2011-12-21 14:54

Hi Tommy,

All checked out on my test server and it should just be down to the field used in the SQL in the widget code; I'd used clicks in the example, which should have been count - in other words this line:

$sql = "SELECT * FROM `".$pto_config_databaseTablePrefix."querylog` ORDER BY clicks DESC LIMIT 10";

...should be:

$sql = "SELECT * FROM `".$pto_config_databaseTablePrefix."querylog` ORDER BY count DESC LIMIT 10";

Cheers,
David.
--
PriceTapestry.com

Submitted by twdesigns on Thu, 2011-12-22 21:21

That did it! Thanks David!

Submitted by koen on Tue, 2014-09-23 14:53

Hi David,

I do have this code installed, it works... But.. in my apache error logs I'm still getting errors when a product has more than one entry... the error looks like:

[Tue Sep 23 16:40:37.373974 2014] [:error] [pid 789257] [client 77.172.181.25:49737] WordPress databasefout Duplicate entry 'India' for key 'query' bij query INSERT INTO `pt_querylog` SET query='India' gemaakt door require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/modernize/page.php'), the_content, apply_filters('the_content'), call_user_func_array, do_shortcode, preg_replace_callback, do_shortcode_tag, call_user_func, pto_core, pto_search, referer: http://www.mysite.com/snel-zoeken/

Can you help me on this one?

Cheers!
Koen

Submitted by support on Tue, 2014-09-23 15:25

Hi Koen,

The mod utilises a unique index to prevent multiple log entries, but the above can be simply recoded using MySQL's ON DUPLICATE KEY syntax.

Where you now have the following replacement code in pto_search.php beginning at line 331:

    $html .= pto_search_html();
    global $pto_log;
    if ($pto_log)
    {
      $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);
    }

REPLACE this with just:

    $html .= pto_search_html();
    global $pto_log;
    if ($pto_log)
    {
      $sql = "INSERT INTO `".$pto_config_databaseTablePrefix."querylog` SET query='".$wpdb->escape($pto_q)."' ON DUPLICATE KEY UPDATE `".$pto_config_databaseTablePrefix."querylog` SET count=count+1 WHERE query='".$wpdb->escape($pto_q)."'";
      $wpdb->query($sql);
    }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by koen on Tue, 2014-09-23 18:25

Hi David,

this doesn't seem to work. No records are added in the databes.
The code I'm using is:

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

Cheers,
Koen

Submitted by support on Wed, 2014-09-24 09:04

Hi Koen,

Have a go with:

    $html .= pto_search_html();
    global $pto_q;
    $parts = explode(":",$pto_q);
    if (!isset($parts[1]) || $parts[1]=="")
    {
      $sql = "SELECT id FROM `".$pto_config_databaseTablePrefix."querylog` WHERE query='".$wpdb->escape($pto_q)."'";
      if (!$wpdb->query($sql))
      {
        $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 koen on Sun, 2014-11-16 12:51

Thanks for this one. Works perfect!

Submitted by twdesigns on Fri, 2016-01-29 16:13

Does this still hold true for the latest version? Thanks!

Submitted by support on Fri, 2016-01-29 16:45

Hi Tommy,

Line numbers updated in original reply for PriceTapestry.org for WordPress version 2.0...

Cheers,
David.
--
PriceTapestry.com

Submitted by Selector84 on Sun, 2016-03-06 20:36

Hi David,

How could this be implemented on the page below the search form on the PT WP plugin.

Was having a play with the code around the below on pto_search.php

<?php
  $html 
str_replace("%ACTION%",get_bloginfo('url').$pto_config_permalink,$html);
  
$html str_replace("%MERCHANTBASEHREF%",get_bloginfo('url').$pto_config_merchantBaseHREF,$html);
  
$html str_replace("%CATEGORYBASEHREF%",get_bloginfo('url').$pto_config_categoryBaseHREF,$html);
  
$html str_replace("%BRANDBASEHREF%",get_bloginfo('url').$pto_config_brandBaseHREF,$html);
  return 
$html;
?>

Managed to get the text out, but struggling with outputting the searches, was playing around with:

<?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>";
?>
from http://www.pricetapestry.com/node/2229 but can see needs more to it to work with WP.

Like the idea of the widget, but could do with just on the page below the search form, would it be easy to make as short code as well?

Thanks,
Sean

Submitted by Selector84 on Sun, 2016-03-06 20:46

Sorry in last post actually should be referencing node http://www.pricetapestry.com/node/892

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

Thanks,
Sean

Submitted by support on Mon, 2016-03-07 09:12

Hi,

Have a go with;

<?php
  $html 
str_replace("%ACTION%",get_bloginfo('url').$pto_config_permalink,$html);
  
$html str_replace("%MERCHANTBASEHREF%",get_bloginfo('url').$pto_config_merchantBaseHREF,$html);
  
$html str_replace("%CATEGORYBASEHREF%",get_bloginfo('url').$pto_config_categoryBaseHREF,$html);
  
$html str_replace("%BRANDBASEHREF%",get_bloginfo('url').$pto_config_brandBaseHREF,$html);
  global 
$pto_config_databaseTablePrefix;
  global 
$wpdb;
  
$html .= "<p>Top Searches</p>";
  
$html .= "<ul>";
  
$sql "SELECT * FROM `".$pto_config_databaseTablePrefix."querylog` ORDER BY count DESC LIMIT 10";
  if (
$wpdb->query($sql))
  {
    foreach(
$wpdb->last_result as $row)
    {
      
$url get_bloginfo('url').$pto_config_permalink."?q=".urlencode($row->query);
      
$html .= "<li><a href='".$url."'>".$row->query."</a></li>";
    }
  }
  
$html .= "</ul>";
  return 
$html;
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Selector84 on Mon, 2016-03-07 23:15

Hi David,

Having trouble getting it working getting error:

Fatal error: Call to a member function query() on a non-object in /../../public_html/wp-content/plugins/pto/pto_search.php on line 268

Had a google and suggested to ensure global $wpdb; is declared but can see it already is http://stackoverflow.com/questions/14078170/fatal-error-call-to-a-member-function-query-on-a-non-object-wordpress-3-5

Thanks,
Sean

Submitted by Selector84 on Mon, 2016-03-07 23:18

David,

Sorry had another go put it again as below

<?php
function pto_search_form()
{
  global 
$pagename;
  global 
$wpdb;
  global 
$pto_q;
  global 
$pto_config_permalink;
  global 
$pto_config_merchantBaseHREF;
  global 
$pto_config_categoryBaseHREF;
  global 
$pto_config_brandBaseHREF;
  global 
$pto_html_search_form;
?>

Sean

Submitted by support on Tue, 2016-03-08 10:01

Hi Sean,

Spotted correctly - global $wpdb; was the missing link, corrected in my original post above.

Cheers,
David.
--
PriceTapestry.com