You are here:  » Cheapest Products and Most Expensive Products

Support Forum



Cheapest Products and Most Expensive Products

Submitted by dbfcs on Tue, 2006-12-12 11:04 in

Is it possible for Price Tapestry to list a page of *all* the products in a database, sorted by most expensive or the cheapest?

My database in question has around 300 products so there will not be a speed issue.

I presume it would be a page called listall.php and it would use the searchresults.php page but am not sure how I would get it to pull all the products from the database sorted by price. The fact that it would have to group up products from various retailers in order to show the cheapest product XYZ or the most expensive product ABC would be difficult, I think. Mind you, when you search for product DEF in Price Tapestry as it stands, it would naturally allow you to sort price ascending or price descending.

I also presume there would be an operator such as &price=asc or &price=desc

The practical application of this is when people are looking for the cheapest or most expensive product around.

Thanks in advance.

Submitted by support on Tue, 2006-12-12 11:16

Hi Dave,

The easiest way to do this is to create a new search modifier (like you have merchant:, brand: etc.) to automatically search for all products, then you can link to the search page directly when you want to list all products.

In search.php look for the switch statement that decides what SQL to construct based on the value of $parts[0], and add the following case:

      case "ALLPRODUCTS":
        $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` GROUP BY name";
        $sqlResultCount = "SELECT COUNT(DISTINCT(name)) as resultcount FROM `".$config_databaseTablePrefix."products`";
        $orderBySelection = $orderByDefault;
        break;

If you are not familiar with PHP's switch statement and can't quite see where to insert this; paste the above code into the line before:

      case "bw":

This means you can now link to your search results as follows to get all products ordered by price descending (or ascending, simply change the URL):

http://www.yoursite.com/search.php?q=ALLPRODUCTS&sort=priceDesc

Finally, if when searching using ALLPRODUCTS you do not want the limit the results but want all products to be displayed you need to override the section of code that adds the LIMIT statement to SQL. This is on line 98 of the distribution as follows:

  $sql .= " LIMIT ".$offset.",".$config_resultsPerPage;

To change this so that it does not limit all products search, try this:

  if ($parts[0] == "ALLPRODUCTS")
  {
    $offset = 1;
    $config_resultsPerPage = 10000;
  }
  $sql .= " LIMIT ".$offset.",".$config_resultsPerPage;

By overriding $config_resultsPerPage this will make sure that the navigation bar is not displayed at the bottom of the results.

Hope this helps,
Cheers,
David.

Submitted by dbfcs on Tue, 2006-12-12 11:27

That is perfect.

I chose not to add the last bit so that the page seperator still works.

Results:

Cheapest Sat Nav Systems
Most Expensive Sat Nav Systems