You are here:  » Some modification regarding to category filter


Some modification regarding to category filter

Submitted by MiTU on Tue, 2020-11-17 23:56 in

Hi David :)

Well as you may know I am always using Price Tapestry for something else but price comparison :)

Things are like this, I dont need category so I changed code a bit.

Removed:

if ($categoryFilter)
  {
    if ($config_useCategoryHierarchy)
    {
      $priceWhere .= " AND categoryid = '".database_safe($categoryFilter)."' ";
    }
    else
    {
      $priceWhere .= " AND category = '".database_safe($categoryFilter)."' ";
    }
  }

Changed category filter, because I am using it together with $q I did it like this and hope I did it right :)

$categoryFilter = (isset($_GET["tag"])?tapestry_normalise($_GET["tag"],":\.\/"):"");

Next I did this:

$qarray = $q.' '.$categoryFilter;
$parts = explode(":",$qarray);

Hope I did it right, because have no idea what am I doing but seems its working fine lol

Now, I am using Category Mapping to add words there, in my case they will work as tags.

Final question is how to join categories and products tables? Want to use it in searchfilters.php

I want to take name from categories table and search description in products table
with that category name and get count, and order by count desc.

Example:

CategoryName (30) - 30 is number of how many products contain word CategoryName in their description.

Hope you understood me lol :D

Thanks David :)

Submitted by support on Wed, 2020-11-18 08:10

Hi,

You can use a LIKE where clause in an SQL statement in html/searchfilters.php to get a count of all products with each name configured in Category Mapping and for any found, generate a drop down with name "tag" to submit back into search.php. For example, something like:

  $sql1 = "SELECT * FROM `".$config_databaseTablePrefix."categories` ORDER BY name";
  database_querySelect($sql1,$rows1);
  $tagNames = array();
  foreach($rows1 as $row1)
  {
    $sql2 = "SELECT COUNT(id) AS numProducts FROM `".$config_databaseTablePrefix."products` WHERE description LIKE '%".database_safe($row1["name"])."%' AND ".$where.$priceWhere." GROUP BY name";
    if (database_querySelect($sql2,$rows2))
    {
      $tagNames[$row1["name"]] = $rows2[0]["numProducts"];
    }
  }
  if (count($tags))
  {
    print "<div class='small-12 medium-2 columns'>";
    print "<label>Tag<br />";
    print "<select name='tag'>";
    foreach($tagNames as $tagName => $count)
    {
      $selected = ($tag==$tagName?"selected='selected'":"");
      print "<option value='".htmlspecialchars($tagName,ENT_QUOTES,$config_charset)."' ".$selected.">".$tagName." (".$count.")</option>";
    }
    print "</select>";
    print "</label>";
    print "</div>";
  }

Hope this points you in the right direction!

Cheers,
David.
--
PriceTapestry.com

Submitted by MiTU on Thu, 2020-11-19 23:12

Thanks David, seems I found a way and works perfect :D

$sql1 = "SELECT c.name, COUNT(*) cnt FROM `".$config_databaseTablePrefix."categories` c INNER JOIN `".$config_databaseTablePrefix."products` p on p.description LIKE CONCAT('%', c.name, '%') WHERE ".$where.$priceWhere." GROUP BY c.id, c.name ORDER BY cnt DESC";

And I added back category filter and edit it a bit for accurate results:

if ($categoryFilter)
{
  $priceWhere .= " AND description LIKE '%".database_safe($categoryFilter)."%' ";
}

Also added back $q:

$parts = explode(":",$q);

Will see what else needs to be done :)

Its hard to do something and you have no idea what you doing haha but its working and I really enjoy doing it :))