You are here:  » Categories in home page


Categories in home page

Submitted by pgabriel on Mon, 2007-01-08 21:51 in

Hy there.

I want to add something like:

<select name="category">
<option value="All">All Categories</option>
<option value=""></option>
</select>

To have the main categories after search field. Could somebody help me?

Thanks

Submitted by support on Mon, 2007-01-08 22:34

Hi Gabriel,

It's not advisable to create a dynamic drop-down of categories because the query to select DISTINCT category values from the database is quite slow. However, if you're happy to hand code a drop down box you can do something quite easily on your search form.

Basically, you need to add a SELECT box, with the name "category" (as in your example above) to the search form in html/searchform.php. In your drop-down, give the "All Categories" an empty value rather than using something like "All", and then the actual category value of any other categories you wish to offer in the drop-down. Then, you need to add some code to the top of search.php, as follows:

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

This will redirect to the normal search results for a "category:widgets" search.

Hope this helps!
Cheers,
David.

Submitted by pgabriel on Mon, 2007-01-08 22:45

I`m stuck. Where in the top of search.php should i put this code?

Thanks,
Gabriel

Submitted by support on Tue, 2007-01-09 08:33

Hi Gabriel,

It goes right at the very top - the very first PHP code there should be after the opening <?php tag....

This is because the behaviour of this code is to look for your new category field in the search form (which is not normally part of how search.php works) and convert the query into the appropriate format, for example: "category:widgets". If you search for "category:widgets" (without the quotes) you will see how the script interprets this as a search for the category widgets.

Cheers,
David.

Submitted by pgabriel on Wed, 2007-01-10 15:20

I have something like this:

<select name="category">
<option value="">All categories</option>
<option value="category:Software:">Software</option>
</select>

and it doesn`t work.

Thanks,
Gabriel

Submitted by support on Wed, 2007-01-10 16:00

Hi Gabriel,

It won't work with that code because the modification i've given you to go at the top of search.php is expecting just a category name in the "category" parameter. You need to use something like this:

<select name="category">
<option value="">All categories</option>
<option value="Software">Software</option>
</select>

Cheers,
David.

Submitted by pgabriel on Wed, 2007-01-10 16:05

Well i don`t know what is happening but, if i make search with "dvd" and select "All categories" gives me some results, if i make that search but selecting "Software" it returns the same results.

Thanks,
Gabriel

Submitted by support on Wed, 2007-01-10 16:26

Hi Gabriel,

It sounds like the code at the top of search.php is not getting triggered, but i've just noticed that it contains an error. It should be as follows

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

That should help, if you're still having trouble feel free to send me the URL to your site (reply to your registration code or forum reg email is the easiest way) and i'll take a look....

Cheers,
David.

Submitted by pgabriel on Wed, 2007-01-10 16:33

Same thing happens.

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

If i dont put for $url=' ' it gives me an error. But still the same results are given back.

Thanks,
Gabriel

Submitted by support on Wed, 2007-01-10 16:35

Hello Gabriel,

Can you send me the URL to your site I will have a look at the redirect and see why it is not correct. Reply to your registration code or forum reg email is the easiest way. Please don't reply to the min max price mods email that I sent eariler as that was from a different address...

Cheers,
David.

Submitted by pgabriel on Wed, 2007-01-10 16:36

I cant`t give you any link because i`m working on localhost. Only next week i will launch the website.

Thanks,
Gabriel

Submitted by support on Wed, 2007-01-10 16:42

Hi Gabriel,

Check your code because it looks like a sprious ' character has got into your version. This will make the line incorrect. Please make sure that it is just this:

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

Now, if it's not working you will need to add some debug code to this to find out why. First of all; try printing the new URL and then existing, to see what it is trying to redirect to:

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

You should see something like this, if you search for "widgets" in the "Software" category:

/search.php?q=category:Software:widgets

What do you see?

If you do not see anything, and the search happens as normal then you need to check your search form, to make sure that the drop down box is correct and has the name "category" so that it is POST'ed to search.php as expected...

Cheers,
David.

Submitted by pgabriel on Wed, 2007-01-10 16:54

It doesn`t work.

Here is my error:

Parse error: syntax error, unexpected '=' in .....search.php on line 4

My line 4 from search.php is the "$url" line. I`ve placed the code at the very top, as you suggested.

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

My form code:

    <input type='text' name='q' size='40' value='<?php print (isset($q)?$q:""); ?>' />
    Min <?php print $config_currencyHTML;?><input type='text' name='minPrice' size='5' value='<?php print ($minPrice?$minPrice:""); ?>' />
    Max <?php print $config_currencyHTML;?><input type='text' name='maxPrice' size='5' value='<?php print ($maxPrice?$maxPrice:""); ?>' />
<select name="category">
<option value="">Any category</option>
<option value="Business">Business</option>
</select>
    <input type='submit' value='<?php print translate("Search"); ?>' />

Thanks,
Gabriel

Submitted by support on Wed, 2007-01-10 17:07

There are still missing characters from the code; here you go:

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

Cheers,
David.

Submitted by pgabriel on Wed, 2007-01-10 17:21

thanks for your support but it doesn`t work at all. I will focus on some other things now, and when i will put it on server i will give you a link in this thread. Maybe then you will know what is going on.

Thanks again,
Gabriel

Submitted by support on Wed, 2007-01-10 17:38

No problem Gabriel.

Feel free to email me the link instead of post to the forum if you wish. When you do; if you could include as an attachment your modified search.php this will enable me to see what's going on.

Cheers,
David.