You are here:  » Top Reviewed Products


Top Reviewed Products

Submitted by knight01 on Tue, 2007-11-13 17:34 in

David,
I'm almost done with the price comparison section of the site, with much thanks to you for the help. Now I'm trying to tie everything together.

I want to put the top rated, most reviewed or even recent reviews of products on a side banner in the main section of the site. (the rest of the site is word press based) Almost like a widget. I found this in my search, http://www.pricetapestry.com/node/1460 , but it's not quite what I'm looking for.

Is there a simple way to do this and link the product reviews to the products in the comparison section? I would think, once the base sql query is created it would be a simple matter of changing the 'order by' 'where' and 'limit' statements to get the differing results... of course I may be completely lost. :)

Submitted by support on Wed, 2007-11-14 09:46

Hi,

The following PHP code can be used outside of the Price Tapestry installation to select certain products and create links to those product pages. I've included example SQL for top rated and most reviewed products in the code below - simply uncomment the one you want. Unfortunately the script does not record review date/time, so last reviewed products is not possible without further modifications.

The only change you will have to make is the first line - $path_to_pricetapestry. If you're not sure what the path is on your server see the instructions in the other thread you mentioned (link) for using phpinfo() to work out the current server path.

<?php
  $path_to_pricetapestry 
"/home/http/sites/example/htdocs/";
  require(
$path_to_pricetapestry."includes/config.php");
  require(
$path_to_pricetapestry."includes/tapestry.php");
  require(
$path_to_pricetapestry."includes/database.php");
  
// ************************
  // UNCOMMENT THE $sql LINE FOR THE PRODUCTS YOU WANT TO DISPLAY
  // 5 highest rated products
  // $sql = "SELECT DISTINCT name FROM `".$config_databaseTablePrefix."products` ORDER BY rating DESC LIMIT 5";
  // 5 most reviewed products
  // $sql = "SELECT DISTINCT name FROM `".$config_databaseTablePrefix."products` ORDER BY reviews DESC LIMIT 5";
  
if (database_querySelect($sql,$rows))
  {
    foreach(
$rows as $product)
    {
      if (
$config_useRewrite)
      {
        
$href $config_baseHREF."product/".tapestry_hyphenate($product["name"]).".html";
      }
      else
      {
        
$href $config_baseHREF."products.php?q=".urlencode($product["name"]);
      }
      print 
"<a href='".$href."'>".$product["name"]."</a><br />";
    }
  }
?>

Hope this helps!
Cheers,
David.

Submitted by knight01 on Thu, 2007-11-15 04:13

David,
Yep, that is what I've got in mind. when implementing it I get the following error:
Warning: main(/home/sitename/public_html/compare/includes/config.php) [function.main]: failed to open stream: No such file or directory in /home/sitename/public_html/wp-content/themes/classic/sidebar.php on line 7

(I changed the real directory name to sitename) I've verified the path is correct using the phpinfo() and copied the path directly from it's result.

I know the failed to open stream is due to it not finding the file, which is why I've tried a couple variations using a ./home/etc/etc and even without the starting slash such as home/etc/etc neither of those seemed to help.

Could this be a chmod issue? Although I think this is running as root, so it should have permissions.

Perfume & Cologne Comparison Site

Submitted by support on Thu, 2007-11-15 09:30

Hi,

Yes - if the path is correct then it is almost certainly a permissions problem. This sounds like you are running the code on a different site rather than within another directory on a site that is running Price Tapestry, so you need to make sure that PHP has access to all "site" folders on your server.

Alternatively, as this hack only requires access to the include files, you could just copy the includes/ directory from Price Tapestry the folder that you want to run this script in; and then just include then instead of:

  require($path_to_pricetapestry."includes/config.php");
  require($path_to_pricetapestry."includes/tapestry.php");
  require($path_to_pricetapestry."includes/database.php");

...just use:

  require("includes/config.php");
  require("includes/tapestry.php");
  require("includes/database.php");

However, if you are running on a different site (host), even if on the same server; later on in the code instead of using $config_baseHREF use the full URL to the other site, so instead of:

 $href = $config_baseHREF."products.php?q=".urlencode($product["name"]);

...use:

 $href = "http://www.example.com/products.php?q=".urlencode($product["name"]);

Cheers,
David.

Submitted by knight01 on Fri, 2007-11-16 04:39

I've actually found the issue. my config.php isn't in the /includes directory it's in the main PT directory. Taht is why it couldn't find it. I tried moving config.php into the include dir and it doesn't seem to find it there. Not sure why... guess it doesn't matter.

Now that it is finding the config it's throwing a sql error:

mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/sitename/public_html/compare/includes/database.php on line 17

I'm not getting this error anywhere on the site except in this added code so I am sure the query is valid. But I don't know exactly what a MySQL-Link resource means...

Perfume & Cologne Comparison Site

Submitted by support on Fri, 2007-11-16 09:22

Hi,

Sorry about that - I should have realised the link to config.php did not have the correct path - you are quite right - it is in the main Price Tapestry directory and not the includes folder...

Now, the error means that the database connection failed, because when it came to try and do the query, it found that the link was invalid.

This may imply that you script is still not including config.php, as this is where the database connection username/password are stored.. To test this, you could try the following in your additional code:

<?php
  
print "Database Username: ".$config_databaseUsername;
?>

If the correct database username is not displayed, check the code at the top of the script that includes the various files. It sounds like you did not need to make a copy of the files, so it should be something like this now if I have understood correctly:

  $path_to_pricetapestry = "/home/sitename/public_html/compare/";
  require($path_to_pricetapestry."config.php");
  require($path_to_pricetapestry."includes/tapestry.php");
  require($path_to_pricetapestry."includes/database.php");

Hope this helps,
Cheers,
David.

Submitted by knight01 on Sat, 2007-11-17 03:46

Added the code to print the database username after the include code but before the query code, it works. DBusername printed correctly.

Yes you are correct with the modification of the code, now it looks as you've listed above with /config.php in the root PT directory.

Still showing the Mysql-link error on the home page of the site. The root of the site is /home/sitename/public_html/ just to point out that I do have PT in a subdir of the site and am running wordpress in the site root.

Let me know if you want ftp access I can setup a temporary guest account to let you poke around.

Perfume & Cologne Comparison Site

Submitted by support on Sat, 2007-11-17 06:20

Hi,

The first thing to do here is to modify includes/database.php to print out the actual MySQL error message when one of the library functions fails. To do this, look for the following code on line 13:

$link = @mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword);

...and add the following code immediately afterwards:

if (!$link)
{
  print mysql_error();exit();
}

Hope this helps,
Cheers,
David.

Submitted by knight01 on Sun, 2007-11-18 03:10

It doesn't print anything as an error, but it does exit. So the if(!link) trap is being called, as it then goes on to the exit function.

I know the dbuser and pass are correct as they work in the /compare/ directory where pt is located. I know it is finding the config.php file as it displays the dbuser name from the test above. What I don't know is why isn't it creating a $link???

Perfume & Cologne Comparison Site

Submitted by support on Sun, 2007-11-18 09:31

Hi,

It is pointing to a permissions error, but this is normally returned by the mysql_error() function. All I can suggest is writing a very quite test script to run in the same directory so that we know it is nothing specific to Price Tapestry includes. To do this, could you try running the version checker script and just confirm that PHP in this directory does have access to MySQL...

test.php

<?php
  $link 
mysql_connect("localhost","username","password");
  print 
"<p>PHP Version ".phpversion()."</p>";
  if (
$link)
  {
    print 
"<p>MySQL Version ".mysql_get_server_info($link)."</p>";
  }
  else
  {
    print 
"<p>Could not connect to MySQL server.</p>";
  }
?>

Change username and password as required, and see if that makes any difference...

Whilst writing this, something else has just occurred to me. If the place you are calling the Price Tapestry includes is actually within a (possibly much) higher level function, it might be necessary to declare the configuration values as global first. To find out if this is the case, where you currently have:

  $path_to_pricetapestry = "/home/sitename/public_html/compare/";
  require($path_to_pricetapestry."config.php");
  require($path_to_pricetapestry."includes/tapestry.php");
  require($path_to_pricetapestry."includes/database.php");

...change it as follows by inserting the global lines just before the call to require config.php...

  $path_to_pricetapestry = "/home/sitename/public_html/compare/";
  global $config_databaseServer;
  global $config_databaseUsername;
  global $config_databasePassword;
  global $config_databaseName;
  global $config_databaseTablePrefix;
  require($path_to_pricetapestry."config.php");
  require($path_to_pricetapestry."includes/tapestry.php");
  require($path_to_pricetapestry."includes/database.php");

...that might just make all the difference...

Hope this helps,
Cheers,
David.

Submitted by knight01 on Mon, 2007-11-19 03:12

David,
It looks like the global declarations solved the issue. I did have to combine the wordpress and pt databases as the databaseName seemed to be conflicting with the wordpress db name variable for some reason.

But all seems to be working.

Perfume & Cologne Comparison Site

Submitted by richard on Mon, 2008-05-26 20:41

Hi David

Hope you had a great bank holiday.

How would you add images to this code?

Regards

Richard

Submitted by support on Tue, 2008-05-27 08:24

Hi Richard,

Bank Holiday wasn't bad - except for the weather!

To add images to this mod, first change the SQL being generated by adding the image_url field to the query; for example:

$sql = "SELECT DISTINCT name,image_url FROM `".$config_databaseTablePrefix."products` ORDER BY reviews DESC LIMIT 5";

Then, in the section where you display the result, you can generate image HTML containing the image URL by using the variable $product["image_url"], so as a complete example:

  $sql = "SELECT DISTINCT name,image_url FROM `".$config_databaseTablePrefix."products` ORDER BY reviews DESC LIMIT 5";
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $product)
    {
      if ($config_useRewrite)
      {
        $href = $config_baseHREF."product/".tapestry_hyphenate($product["name"]).".html";
      }
      else
      {
        $href = $config_baseHREF."products.php?q=".urlencode($product["name"]);
      }
      print "<img src='".$product["image_url"]."' /><br />";
      print "<a href='".$href."'>".$product["name"]."</a><br />";
    }
  }

Cheers,
David.

Submitted by richard on Tue, 2008-05-27 13:30

Hi David

Your solution works except that I am getting the same product reported multiple times. The reason for this is that I may have five merchants with the same product mapped to one product and each review is repeated four or five times as the image url is different for each merchant.

What would you do to the sql line to reference the image of the cheapest product?

Regards

Richard

Submitted by support on Tue, 2008-05-27 13:39

Hi Richard,

Try this as an alternative...

  $sql = "SELECT name,image_url FROM `".$config_databaseTablePrefix."products` GROUP BY name ORDER BY reviews DESC LIMIT 5";

Cheers,
David.

Submitted by richard on Tue, 2008-05-27 13:43

Hi David

Yep that sure works

Many thanks

PS don't forget the donate button

Submitted by support on Wed, 2008-06-11 08:08

Richard wrote:
=============================================

Hi David

My price tapestry is installed in a number of directories and I would like to pull featured products into the home page from the various directories.

What code should I use so that I can feature more than one installation

E.g. i have tried

function destroy_path_to_pricetapestry()
{
global $path_to_pricetapestry;
unset($path_to_pricetapestry);
}

without success

Any help much appreciated

Regards

Richard
=============================================

Submitted by support on Wed, 2008-06-11 08:15

Hi Richard,

The easiest thing to do would be to call in the config.php of each installation before the code to select and display featured products; and to make it neater still; perhaps wrap the featured products code into a function; and then call that after each config file. Here's a complete example to demonstrate what I mean - cut and paste the parts you need.... (installA, installB, installC are the folders containing each installation)

<?php
  
// these files just need to be included from one installation
  
require("installA/includes/tapestry.php");
  require(
"installA/includes/database.php");
  function 
featured($installation)
  {
    require(
$installation/config.php);
    
$sql "SELECT * FROM `".$config_databaseTablePrefix."featured` ORDER BY sequence";
    if (
database_querySelect($sql,$rows))
    {
      
$sqlNames = array();
      
$sqlCase "CASE name";
      foreach(
$rows as $featured)
      {
        
$sqlNames[] = "'".$featured["name"]."'";
        
$sqlCase .= " WHEN '".database_safe($featured["name"])."' THEN ".$featured["sequence"];
      }
      
$sqlCase .= " END AS sequence";
      
$sqlIn implode(",",$sqlNames);
      
$sql "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, ".$sqlCase." FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name ORDER BY sequence";
      
database_querySelect($sql,$rows);
      
$featured["products"] = $rows;
      foreach(
$featured["products"] as $k => $product)
      {
        if (
$config_useRewrite)
        {
          
$featured["products"][$k]["productHREF"] = "product/".tapestry_hyphenate($product["name"]).".html";
          
$featured["products"][$k]["reviewHREF"] = "review/".tapestry_hyphenate($product["name"]).".html";
        }
        else
        {
          
$featured["products"][$k]["productHREF"] = "products.php?q=".urlencode($product["name"]);
          
$featured["products"][$k]["reviewHREF"] = "reviews.php?q=".urlencode($product["name"]);
        }
      }
    }
    require(
$installation/html/featured.php);
  }
  
featured("installA");
  
featured("installB");
  
featured("installC");
?>

...something like that!

Cheers,
David.