You are here:  » search a category search box

Support Forum



search a category search box

Submitted by wilkins on Thu, 2007-10-04 14:48 in

hi

I am building a site that only has 3 or 4 categories however there will be a lot of items in each category. I dony want to split them up into sub folders, is there any way just to have a search box for just one of the categories or a drop down system that could pick one of the categories

Thanks

brent

Submitted by support on Thu, 2007-10-04 15:04

Hi Brent,

Yes, that's quite straight forward to do. A simple modification to search.php can be made, which will allow you to pass in a category name in the URL (e.g. ?category=Clothing ), and then you can either fix that category using a hidden form field, or use a select box.

To make the changes in search.php, look for the following code at the top of the script (line 4 in the distribution):

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

Replace this with:

$q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");
$category = $_GET["category"];
if ($category)
{
  $q = "category:".$category.":".$q;
}

Now, to pass a fixed category through from any page that has a search form on it, you can simply add the following to your search form (the default search form is in html/searchform.php):

<input type='hidden' name='category' value='Clothing' />

Alternatively, to make it a drop down box; use:

<select name='category'>
<option value='Clothing'>Clothing</option>
<option value='Electrical'>Electrical</option>
<option value='Home'>Home and Garden</option>
</select>

Hope this helps,
Cheers,
David.

Submitted by italiano28 on Thu, 2008-02-07 10:06

I try it before i contact you but i get just search box and a select option that work separately :D,i don t know where exatly i must put the code,i know in the formsearch but exatly where ?

<select name='category'>
<option value='Clothing'>Clothing</option>
<option value='Electrical'>Electrical</option>
<option value='Home'>Home and Garden</option>
</select>

Im not expert with php,if u can ghive me the complete script that i can put in my formsearch,thank you very mutch.

Submitted by support on Thu, 2008-02-07 10:09

Hi,

At the moment, html/searchform.php is this:

<div class='searchform'>
  <form name='search' action='<?php print $config_baseHREF ?>search.php'>
    <input type='text' name='q' size='35' value='<?php print (isset($q)?$q:""); ?>' />
    <input type='submit' value='<?php print translate("Search"); ?>' />
  </form>
</div>

...change it to:

<div class='searchform'>
  <form name='search' action='<?php print $config_baseHREF ?>search.php'>
    <input type='text' name='q' size='35' value='<?php print (isset($q)?$q:""); ?>' />
    <select name='category'>
      <option value='Clothing'>Clothing</option>
      <option value='Electrical'>Electrical</option>
      <option value='Home'>Home and Garden</option>
    </select>
    <input type='submit' value='<?php print translate("Search"); ?>' />
  </form>
</div>

Of course, you want to change the categories to your own (and add a couple more options). Don't forget the changes in search.php as well, if you're not sure how to do that, you can email me your search.php and I will add the change for you.

Cheers,
David.

Submitted by italiano28 on Thu, 2008-02-07 10:38

Thank you very mutch,very fast, i have rss.php like this http://www.pricetapestry.com/node/1110 and work fine,how can i add an extra button to this search form for get feed? "search button" align and "get rss button"
And thank you again.

Submitted by support on Thu, 2008-02-07 10:46

Hi,

You can just add a link directly to the feed if that's what you want to do, for example:

<a href='/rss.php'>Get RSS</a>

Cheers,
David.

Submitted by italiano28 on Thu, 2008-02-07 10:54

I need just to use same search text form to get feed from the search result,the query that my script rss use is: rss.php?q=query , if anyone want just search,click on search button but if he want get rss feed from that search click on the rss button,i just need to add the button rss for submit te same text search form. Thank you.

Submitted by support on Thu, 2008-02-07 11:02

Hi,

The easiest way to do this is to redirect from search.php to rss.php if they click the Get RSS button. In your html/searchform.php where you have:

    <input type='submit' value='<?php print translate("Search"); ?>' />

...change this to:

    <input type='submit' value='<?php print translate("Search"); ?>' />
    <input type='submit' name='submit' value='Get RSS' />

Then, at the top of search.php, where you added this:

$q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");
$category = $_GET["category"];
if ($category)
{
  $q = "category:".$category.":".$q;
}

...add this as well:

if ($_GET["submit"] == "Get RSS")
{
  header("Location: rss.php?q=".urlencode($q));
  exit();
}

Cheers,
David.