You are here:  » More search features


More search features

Submitted by pgabriel on Sat, 2007-01-13 13:53 in

Hello,

I`m working on a project right now and i wish to add more search features (fields) in search.php

Help needed.
Thanks in advance,
Gabriel

Submitted by support on Sat, 2007-01-13 14:00

Hello Gabriel,

Be careful with this sort of feature because you can easily run into performance problems when searching on fields that are not indexed in the databased.

Have you added new fields to your database that you wish to search on; or do you just want to extend search to existing fields (description, category, brand etc.)?

Searching on description is made more complicated by the fact that a full text index would need to be created. This can be done; have a look at the following thread for more information:

http://www.pricetapestry.com/node/224

If you can let me know more about what you want to do I will be able to provide more help for searching other fields etc..

Cheers,
David.

Submitted by pgabriel on Sat, 2007-01-13 14:02

Yes I wish to add more fields in my database and then call them from search.

Thanks,
Gabriel

Submitted by pgabriel on Sat, 2007-01-13 14:04

Let`s say i have the field "year" in database. What should i have to do to call it from a drop down menu (let`s say year "1999") and give the search results i need.

Thanks,
Gabriel

Submitted by pgabriel on Sat, 2007-01-13 14:05

Can i write you an email of what exactly i wish to do?

Thanks,
Gabriel

Submitted by support on Sat, 2007-01-13 14:10

Hi,

Adding new fields is quite complex, there are lots of modifications that you need to make to the database and admin scripts etc. The instructions for adding new fields are in this thread:

http://www.pricetapestry.com/node/313

Once you've added the new fields; before creating specific search forms for them you would want to copy the way the category and brand search works in search.php. You will see the following code, starting on line 36:

      case "merchant":
        // pass through to category
      case "category":
        // pass through to brand
      case "brand":
        $where = " ".$parts[0]."='".database_safe($parts[1])."' ";

This is the code that supports the search query format "category:widgets" for example. You would need to add a CASE statement for your new field, using exactly the same name as the new field that you have added to your database. For example, lets suppose you have added a "model" field, your code would look like this:

      case "model":
      case "merchant":
        // pass through to category
      case "category":
        // pass through to brand
      case "brand":
        $where = " ".$parts[0]."='".database_safe($parts[1])."' ";

(notice your new field added at the top of the list)

Once this is in place, you can create a search form with a field called "model", and use almost the same code as in your recent thread regarding categories on your homepage....

http://www.pricetapestry.com/node/772

for example:

  if ($_POST["model"])
  {
    $url = $config_baseHREF."search.php?q=model:".$_POST["model"].":".$_POST["q"];
    print $url;exit();
    header("Location: ".$url);
    exit();
  }

Hope this helps,
Cheers,
David.

Submitted by support on Sat, 2007-01-13 14:12

Hi again,

Just seen your other comment about adding a "year" field; you could do this in exactly the same way as the category drop down as described in the other thread...

http://www.pricetapestry.com/node/772

Cheers,
David.

Submitted by suffolk66 on Tue, 2012-01-31 10:51

I'm trying to create a search function that enables a user to select a brand and then a model. Once the brand is selected, the model drop down is populated with the corresponding models. Both Product and Model fields are registered and imported.

This page seems to partly answer the question but I'm not sure what coding changes to make to enable the model drop-down to be populated.

Submitted by support on Tue, 2012-01-31 13:11

Hi suffolk66;

The first stage in implementing this would be to extract the search.php from the Sidebar Filters mod available from this page.

Next, you would need to duplicate the filter code to create a new modelFilter. To do this, using your text editor search in search.php for "brandFilter" (without quotes) and copy each block of code and then paste in at the same point in the code (e.g. just below the brandFilter block) replacing brandFilter with modelFilter.

With that in place, a hierarchical search form can be constructed. Have a go with something like this, which you could paste into your site where required; or use as a replacement for html/searchform.php (if the point at which you wish to add the code is within a PHP section don't forget to come out PHP mode with the closing ?> tag and re-open afterwards with the opening <?php tag)

<div class='searchform'>
<?php
  if ($_GET["brandFilter"])
  {
    $action = $config_baseHREF."search.php";
  }
  else
  {
    $action = "";
  }
?>
<form method='GET' action='<?php print $action?>'>
<input type='hidden' name='q' value='bw:' />
<?php
  $sql = "SELECT DISTINCT(brand) FROM `".$config_databaseTablePrefix."products` WHERE brand <> '' ORDER BY brand";
  database_querySelect($sql,$rows);
  print "<select name='brandFilter'>";
  print "<option value=''>Choose Brand...</option>";
  foreach($rows as $row)
  {
    $selected = ($_GET["brandFilter"]==$row["brand"]?"selected='selected'":"");
    print "<option value='".htmlentities($row["brand"],ENT_QUOTES,$config_charset)."' ".$selected.">".$row["brand"]."</option>";
  }
  print "</select>";
  if ($_GET["brandFilter"])
  {
  $sql = "SELECT DISTINCT(model) FROM `".$config_databaseTablePrefix."products` WHERE model <> '' AND brand = '".database_safe($_GET["brandFilter"])."' ORDER BY model";
  database_querySelect($sql,$rows);
  print "<select name='modelFilter'>";
  print "<option value=''>Choose Model...</option>";
  foreach($rows as $row)
  {
    print "<option value='".htmlentities($row["model"],ENT_QUOTES,$config_charset)."'>".$row["model"]."</option>";
  }
  print "</select>";
  }
?>
<input type='submit' value='Go' />
</form>
</div>

...should be pretty close!

Cheers,
David.
--
PriceTapestry.com

Submitted by suffolk66 on Tue, 2012-01-31 14:18

Thanks for your help. I've changed the code in my search page to incorporate the model as follows:

{code saved}

I've uploaded the revised searchform too but I get this error when I try to run it:

Not Found
The requested URL /".$action." was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Submitted by support on Tue, 2012-01-31 14:26

Hi,

Your changes in search.php are perfect.

I made a mistake in the form code which is why you are getting the not found error; this line:

<form method='GET' action='".$action."'>

...should actually be:

<form method='GET' action='<?php print $action?>'>

...as the script isn't in PHP mode at that point... that should be all it is!

(also corrected in OP above)

Cheers,
David.
--
PriceTapestry.com