You are here:  » Please help with the search query...

Support Forum



Please help with the search query...

Submitted by paul30 on Tue, 2008-09-23 07:55 in

Hello, I want to display a list of all distinct categories of all the products that are shown after user has searched for something...

Ok let me explain it again: User has searched for a keyword and got to the search.php page that displays the list of products - I want to get a list of categories those products are in!

I was trying to get it done all day yesterday, but I just can not get this query!!! :(

So far I got this:

$sql = "SELECT DISTINCT category FROM `".$config_databaseTablePrefix."products` WHERE name =

Please help .... :(

Pasha

Submitted by support on Tue, 2008-09-23 08:02

Hello Pasha,

To do this, you would need to mirror the main search SQL's "WHERE" clause exactly. Luckily, the variable $where is not used anywhere else in search.php after the main query has taken place - so you can use it in your category query! For example:

<?php
  $sql 
"SELECT DISTINCT(category) AS category FROM `".$config_databaseTablePrefix."products` WHERE ".$where;
  if (
database_querySelect($sql,$rows))
  {
    foreach(
$rows as $row)
    {
      print 
"<li>".$row["category"]."</li>";
    }
  }
?>

Hope this helps...

Cheers,
David.

Submitted by paul30 on Tue, 2008-09-23 08:43

No... It gives me the MYSQL error in database.php line 21

However your line of thinking got me somewhere: There seems something weird going on in my search.php code because I just added :

if ($parts[0] != "category" && $parts[0] != "brand" && $parts[0] != "merchant")
{
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $row)
    {
      $fcats[$row["category"]] = $row["category"];
    }
  }
right after the: require("html/banner.php"); and I am getting the needed category list even without the actual query!

I am not a programer, and it is hard for me to explain why is it happening like that, but it works!

Thanks!

Submitted by support on Tue, 2008-09-23 09:55

Hi,

That would make sense actually - the $where variable is only set in a way that can be used like this for a "normal" search, i.e. whenever it is not a category:, brand: or merchant: search...!

Cheers,
David.