You are here:  » Category drop down

Support Forum



Category drop down

Submitted by stevewales20 on Sun, 2012-11-11 22:48 in

Hi david,

Been a while, hope everything is good on your end.

So the problem i'm having is with a javascript drop down i'm using. I have used the side bar filters that you sent me a while back, and everything is working correctly with that. The issue i'm having is sending the category drop down value to the search.php page.

<input type='hidden' id='category' name='categoryFilter[]' value='0' />

The above seems to work ok for unless all categories is selected which uses the value of 0.

The other issue i'm having is, ive created a hierarchy of categories and have sub categories below them. The drop down box is setup to only select the top categories. I can call the other sub categories by using the parent id.

I can't seem to find where to edit the search.php code to allow me for example to search ignoring the category filters if the searchform is used and then load the category filters when it's completed.

I hope this makes sense. I'm not very good at explaining myself, i tend to babble on :(

Thanks
Steve

Submitted by support on Mon, 2012-11-12 09:08

Hi Steve,

When form fields are named using fieldname[] this instructs PHP to treat that incoming $_GET variable as an array; so it sounds like your JavaScript to update the hidden field actually needs to be creating multiple hidden fields for each selected categoryFilter value, perhaps using document.write. Your search <form> HTML would need a SPAN element so that you can access its innerHTML property in order to write the hidden fields; e.g.

<span id='category'></span>

And then whilst this would be done computationally within a loop, the result would be equivalent to:

var category = document.getElementById('category');
category.innerHTML;
category.innerHTML = category.innerHTML + "<input type='hidden' name='category[]' value='Category 1'>";
category.innerHTML = category.innerHTML + "<input type='hidden' name='category[]' value='Category 2'>";
category.innerHTML = category.innerHTML + "<input type='hidden' name='category[]' value='Category 3'>";

PHP will then see the categoryFilter[] array as containing Category 1, Category 2 and Category 3.

Regarding getting search.php to ignore category filters, look for the block of code that generates the $priceWhere variable; however I wasn't quite sure what you meant regarding loading the filters when it's completed so if you're still not sure perhaps if you could give an example (even on a URL if you want - i'll remove before publishing your reply) just let me know...

Cheers,
David.
--
PriceTapestry.com

Submitted by stevewales20 on Mon, 2012-11-12 19:32

Thanks for the response david.

I appologise about the topic of the site in advance :)

{link saved} (development server)

I've overcome the first problem of not specifying the category and doing a broad search. I simply removed the value from the submission and it bypassed the category filter and just searched all categories.

So basically i'm after some advice.

{link saved}

As you can see the top category is selected and highlighted correctly, what i'm after is to pull in the sub categories as well in a seperate block below. I'm just wondering the best way to pull in additional data by using the categoryFilter[] value.

All the sub categories have a parent id which = the id of top category.

Hopefully i explained myself a little better this time and some examples may help.

Thanks again for you time.
Steve

Submitted by support on Tue, 2012-11-13 09:05

Hi Steve,

The actual implementation would depend on the structure of your category hierarchy table. Assuming a table `cats` where a parent_id of 0 indicates a top level category then you could create the sub-category select clause as follows;

<?php
  
if (count($categoryFilter))
  {
    
$ins = array();
    foreach(
$categoryFilter as $v)
    {
      
$ins[] = "'".database_safe($v)."'";
    }
    
$in implode(",",$ins);
    
$sql "SELECT name FROM `cats` WHERE parent_id IN (SELECT id FROM `cats` WHERE name IN (".$in."))";
    if (
database_querySelect($sql,$subcats))
    {
      
// use $subcats result
    
}
  }
?>

I may not be quite with you yet i'm afraid; feel free to post more examples or if you're still not sure let me know more info about how you have set-up your category/subcategory system...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stevewales20 on Tue, 2012-11-13 17:56

Hi David,

Appreciate the help.

I have a table setup called categories.

The table contains an id, name, parentid.

The top categories have the value of NULL. The subcategories then contain the id's of the top categories.

So if for example i click on the search 'white widgets'. The id is used to pull in all categories with the id of the parent.

So much going on in the search results i'm not sure where i am with it :( The above code i'm assuming is placed into the searchresults.php?

Could you break down the above code for me, i can't seem to follow it. Sorry been a while since i last coded in php.

Steve

Submitted by stevewales20 on Tue, 2012-11-13 22:48

Hi David,

I've worked a little on this. So far i have this:

http://www.example.com/Working/SUBCATEGORYCategorytest.php

    $sql1 = "SELECT name, id FROM `".$config_databaseTablePrefix."categories` WHERE parentid is NULL LIMIT 4";
    //$sql1 = "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE ".$where." AND category <> '' ORDER BY category";
    if (database_querySelect($sql1,$rows1))
    {
      foreach($rows1 as $row)
      {
        $checkboxId++;
$checked = 'checked';
        print "<input id='cb".$checkboxId."' type='checkbox' onclick='document.forms[0].submit()' name='categoryFilter[]' value='".htmlentities($row["name"],ENT_QUOTES,$config_charset)."' ".$checked." /> <label for='cb".$checkboxId."'><a href='&amp;categoryFilter[]=".urlencode($row["name"])."'>".$row["name"]."</a></label>";
        print "<br />";
        //Grab all sub categories using the parent id.
        $sql1 = "SELECT name FROM `".$config_databaseTablePrefix."categories` WHERE parentid = ".$row["id"]." LIMIT 3";
        if (database_querySelect($sql1,$rows2))
     {
       foreach($rows2 as $row)
       {
         $checkboxId++;
         print "---<input id='cb".$checkboxId."' type='checkbox' onclick='document.forms[0].submit()' name='subCategoryFilter[]' value='".htmlentities($row["name"],ENT_QUOTES,$config_charset)."' ".$checked." /> <label for='cb".$checkboxId."'><a href='&amp;subCategoryFilter[]=".urlencode($row["name"])."'>".$row["name"]."</a></label>";
print "<br />";
         }
         }
      }
      print "<br /><br />";
    }

I've broken apart the code from the searchresults and used that as a basis. Hoping that you can help me a little now i have explained this a little more.

The issue i have is getting this to work on the products table to be apart of the filter results.

what i'm aiming for on the filtered results:

1. user is presented with all parent categories in which the search word is found.
2. user clicks on parent categories to be shown the sub categories.

I totally appreciate that this is a little offtopic and i'am really grateful.

hopefully that explains it a little better?

Thanks
Steve

Submitted by support on Wed, 2012-11-14 09:35

Hi Steve,

Have you implemented subcategory filtering in search.php as yet in order to support this? I think that would be the first stage if not as there is no subcategory field on the database in the distribution.

Is this something you have added as yet or would that be the first stage, in order to achieve a search URL e.g.

/search.php?q=widgets&categoryFilter[]=Foo&subcategoryFilter[]=Bar

Cheers,
David.
--
PriceTapestry.com

Submitted by stevewales20 on Wed, 2012-11-14 09:58

Hi David,

The sub categories are mapped to the category field along with the categories . Currently there's nothing to specify if its a sub category. I'll need to sort this out so a field distinguishes if its a a sub category.

You helped me with a subcategory filtering code some time ago. I'll keep plugging away and come back for feedback and some help if that's ok.

Thanks
Steve