Loading


Advertisement:

Author Topic: Category and Sub-category dropdown in search  (Read 4832 times)

tomshaft

  • Hero Member
  • *****
  • Posts: 862
  • Osshaft for Osclass - Add 650 posts to my total
Category and Sub-category dropdown in search
« on: October 30, 2013, 02:26:09 am »
I know this has been asked many, many times because I have done an extensive search on both the forum and Google but no reply has answered this. I also know the answer likely is simple but lately I've been brain dead and can't seem to figure it out.
 
In inc.search.php I want just the search term box, a categories dropdown box and a sub-categories dropdown box then the submit button. No region or city. I know when I'm looking to buy something in this very small world I rarely care where it is. All I care is to quickly and easily find it.
 
Thanks in advance.
Tom
 
 

teseo

  • Hero Member
  • *****
  • Posts: 5686
Re: Category and Sub-category dropdown in search
« Reply #1 on: October 30, 2013, 01:43:26 pm »
Hi Tom,

This I adapted it from Repurpose method to only show subcategory selector once the parent category has been selected, here I've modified a line in the style sheet to always show both selectors:

Theme functions.php:

Code: [Select]
class custItemForm extends ItemForm
{
<?php
    
static public function cust_category_two_selects($categories null$item null$default_item null$parent_selectable false)
    {

        
$categoryID Params::getParam('catId');
        if( 
osc_item_category_id() != null ) {
            
$categoryID osc_item_category_id();
        }

        if( 
Session::newInstance()->_getForm('catId') != '' ) {
            
$categoryID Session::newInstance()->_getForm('catId');
        }

        
$subcategoryID '';
        if( !
Category::newInstance()->isRoot($categoryID) ) {
            
$subcategoryID $categoryID;
            
$category      Category::newInstance()->findRootCategory($categoryID);
            
$categoryID    $category['pk_i_id'];
        }

        if(
$categories == null) {
            if(
View::newInstance()->_exists('categories')) {
                
$categories View::newInstance()->_get('categories');
            } else {
                
$categories osc_get_categories();
            }
        }

        if (
$item == null) { $item osc_item(); }

        
$subcategory = array();
        
?>

    <select id="parentCategory" name="parentCatId">
        <option value=""><?php _e('Select a category''modern'); ?></option>
        <?php foreach($categories as $_category) {
        
$selected = ( (isset($item["fk_i_category_id"]) && $item["fk_i_category_id"] == $_category['pk_i_id']) || (isset($categoryID) && $categoryID == $_category['pk_i_id']) );
        if(
$selected) { $subcategory $_category; };
        echo 
'<option value="'.$_category['pk_i_id'].'" '.($selected 'selected="selected"' '' ).'>'.$_category['s_name'].'</option>';
    } 
?>

    </select>
    <select id="catId" name="catId">
        <?php
        
if(!empty($subcategory)) {
            if( 
count($subcategory['categories']) > ) {
                echo 
'<option value="">'.__('Select Subcategory''modern').'</option>'
                foreach(
$subcategory['categories'] as $c) {
                    
$selected = ( (isset($item["fk_i_category_id"]) && $item["fk_i_category_id"] == $c['pk_i_id']) || (isset($subcategoryID) && $subcategoryID == $c['pk_i_id']) );
                    echo 
'<option value="'.$c['pk_i_id'].'" '.($selected 'selected="selected"' '' ).'>'.$c['s_name'].'</option>';
                }
            } else {
                echo 
'<option value="'.$categoryID.'" >'._e('No Subcategory''modern').'</option>';
            }
        } else {
            echo 
'<option value="">'._e('Select Subcategory''modern').'</option>';
        }
        
?>

    </select>
    <script type="text/javascript" charset="utf-8">
            <?php
            
foreach($categories as $c) {
                if( 
count($c['categories']) > ) {
                    
$subcategory = array();
                    for(
$i 0$i count($c['categories']); $i++) {
                        
$subcategory[] = array($c['categories'][$i]['pk_i_id'], $c['categories'][$i]['s_name']);
                    }
                    
printf('var categories_%1$s = %2$s;'$c['pk_i_id'], json_encode($subcategory));
                    echo 
PHP_EOL;
                }
            }
            
?>

        $(document).ready(function(){
            $("#parentCategory").bind('change', function(){
                var categoryID = $(this).val();
                if( categoryID == 0 ) {
                    var options = '<option value="' + categoryID + '" selected=""><?php _e('No Subcategory''modern'); ?></option>';
                }
                categories = window['categories_' + categoryID];
                if( categories==null || !$.isArray(categories) ) {
                    var options = '<option value="' + categoryID + '" selected=""><?php _e('No Subcategory''modern'); ?></option>';
                } else {
                    var options = '<option value="' + categoryID + '" selected=""><?php _e('Select Subcategory''modern'); ?>...</option>';
                    $.each(categories, function(index, value){
                        options += '<option value="' + value[0] + '">' + value[1] + '</option>';
                    });
                };
                $('#catId').html(options);
                $("#catId").next("a").find(".select-box-label").text('Select a Sub Category...');
                $("#catId").change();
                $("div#uniform-catId.selector").css('visibility', 'visible');
            });

            // In case of error, if Category already chosen, let's show the Subcategory selector
            if($("div#uniform-parentCategory.selector select").val()) {
                $("div#uniform-catId.selector").css('visibility', 'visible');
            }

        });

    </script>
    <?php
    
}
}
?>

style.css (or your own style sheet):

Code: [Select]
/*Subcategories 'a la Repurpose'*/
div#uniform-parentCategory.selector {
    width: 238px;
    margin-right: 3px;
}
div#uniform-parentCategory.selector span {
    width: 205px;
}
div#uniform-parentCategory.selector select {
    min-width: 205px;
    width: 238px;
}
div#uniform-catId.selector {
    /* visibility: hidden; */ /* Original, subcategory selector is hidden until a parent category is selected */
    visibility: visible; /* That's for you, you want this always visible for a search bar */
    width: 238px;
}
div#uniform-catId.selector span {
    width: 205px;
}
div#uniform-catId.selector select {
    min-width: 205px;
    width: 238px;
}

inc.search.php

Code: [Select]
<?php custItemForm::cust_category_two_selects(nullnull__('Select a category''modern')); ?>

Regards

tomshaft

  • Hero Member
  • *****
  • Posts: 862
  • Osshaft for Osclass - Add 650 posts to my total
Re: Category and Sub-category dropdown in search
« Reply #2 on: October 30, 2013, 09:08:20 pm »
teseo,
 
Tried the code but fails at inc.search but you have the right stuff and it is a great answer and have pointed me in the right direction. Thank you very much.
 
More important is just who are you? What are you? Maybe your the Google of Osclass because all we have to do is type in a "search term" and you pop up with great answers. How do you know so much about osclass code.
You came out of no where with absolutely great help and answers. You have helped hundreds, maybe thousands. I have great respect.
 
Thank you
Tom

teseo

  • Hero Member
  • *****
  • Posts: 5686
Re: Category and Sub-category dropdown in search
« Reply #3 on: October 31, 2013, 07:59:35 pm »
Thanks Tom, you are too kind. :) Well, I'm just a sort of a jack of all computer trades, but I have been like that for almost 30 years now, so one ends learning enough to pass as an specialist. :D Anyway, the greater part of that knowledge came from others who shared theirs, so I'm just trying to contribute to the flow a bit. ;)

Best regards
« Last Edit: October 31, 2013, 08:01:37 pm by teseo »

deepakvarma

  • Jr. Member
  • **
  • Posts: 97
Re: Category and Sub-category dropdown in search
« Reply #4 on: October 31, 2013, 08:53:15 pm »
for more category customization open
oc-includes > osclass > frm >Item.form.class.php

arifur

  • Newbie
  • *
  • Posts: 17
Re: Category and Sub-category dropdown in search
« Reply #5 on: November 12, 2013, 02:31:14 pm »
Hi Tom,

This I adapted it from Repurpose method to only show subcategory selector once the parent category has been selected, here I've modified a line in the style sheet to always show both selectors:

Theme functions.php:

Code: [Select]
class custItemForm extends ItemForm
{
<?php
    
static public function cust_category_two_selects($categories null$item null$default_item null$parent_selectable false)
    {

        
$categoryID Params::getParam('catId');
        if( 
osc_item_category_id() != null ) {
            
$categoryID osc_item_category_id();
        }

        if( 
Session::newInstance()->_getForm('catId') != '' ) {
            
$categoryID Session::newInstance()->_getForm('catId');
        }

        
$subcategoryID '';
        if( !
Category::newInstance()->isRoot($categoryID) ) {
            
$subcategoryID $categoryID;
            
$category      Category::newInstance()->findRootCategory($categoryID);
            
$categoryID    $category['pk_i_id'];
        }

        if(
$categories == null) {
            if(
View::newInstance()->_exists('categories')) {
                
$categories View::newInstance()->_get('categories');
            } else {
                
$categories osc_get_categories();
            }
        }

        if (
$item == null) { $item osc_item(); }

        
$subcategory = array();
        
?>

    <select id="parentCategory" name="parentCatId">
        <option value=""><?php _e('Select a category''modern'); ?></option>
        <?php foreach($categories as $_category) {
        
$selected = ( (isset($item["fk_i_category_id"]) && $item["fk_i_category_id"] == $_category['pk_i_id']) || (isset($categoryID) && $categoryID == $_category['pk_i_id']) );
        if(
$selected) { $subcategory $_category; };
        echo 
'<option value="'.$_category['pk_i_id'].'" '.($selected 'selected="selected"' '' ).'>'.$_category['s_name'].'</option>';
    } 
?>

    </select>
    <select id="catId" name="catId">
        <?php
        
if(!empty($subcategory)) {
            if( 
count($subcategory['categories']) > ) {
                echo 
'<option value="">'.__('Select Subcategory''modern').'</option>'
                foreach(
$subcategory['categories'] as $c) {
                    
$selected = ( (isset($item["fk_i_category_id"]) && $item["fk_i_category_id"] == $c['pk_i_id']) || (isset($subcategoryID) && $subcategoryID == $c['pk_i_id']) );
                    echo 
'<option value="'.$c['pk_i_id'].'" '.($selected 'selected="selected"' '' ).'>'.$c['s_name'].'</option>';
                }
            } else {
                echo 
'<option value="'.$categoryID.'" >'._e('No Subcategory''modern').'</option>';
            }
        } else {
            echo 
'<option value="">'._e('Select Subcategory''modern').'</option>';
        }
        
?>

    </select>
    <script type="text/javascript" charset="utf-8">
            <?php
            
foreach($categories as $c) {
                if( 
count($c['categories']) > ) {
                    
$subcategory = array();
                    for(
$i 0$i count($c['categories']); $i++) {
                        
$subcategory[] = array($c['categories'][$i]['pk_i_id'], $c['categories'][$i]['s_name']);
                    }
                    
printf('var categories_%1$s = %2$s;'$c['pk_i_id'], json_encode($subcategory));
                    echo 
PHP_EOL;
                }
            }
            
?>

        $(document).ready(function(){
            $("#parentCategory").bind('change', function(){
                var categoryID = $(this).val();
                if( categoryID == 0 ) {
                    var options = '<option value="' + categoryID + '" selected=""><?php _e('No Subcategory''modern'); ?></option>';
                }
                categories = window['categories_' + categoryID];
                if( categories==null || !$.isArray(categories) ) {
                    var options = '<option value="' + categoryID + '" selected=""><?php _e('No Subcategory''modern'); ?></option>';
                } else {
                    var options = '<option value="' + categoryID + '" selected=""><?php _e('Select Subcategory''modern'); ?>...</option>';
                    $.each(categories, function(index, value){
                        options += '<option value="' + value[0] + '">' + value[1] + '</option>';
                    });
                };
                $('#catId').html(options);
                $("#catId").next("a").find(".select-box-label").text('Select a Sub Category...');
                $("#catId").change();
                $("div#uniform-catId.selector").css('visibility', 'visible');
            });

            // In case of error, if Category already chosen, let's show the Subcategory selector
            if($("div#uniform-parentCategory.selector select").val()) {
                $("div#uniform-catId.selector").css('visibility', 'visible');
            }

        });

    </script>
    <?php
    
}
}
?>

style.css (or your own style sheet):

Code: [Select]
/*Subcategories 'a la Repurpose'*/
div#uniform-parentCategory.selector {
    width: 238px;
    margin-right: 3px;
}
div#uniform-parentCategory.selector span {
    width: 205px;
}
div#uniform-parentCategory.selector select {
    min-width: 205px;
    width: 238px;
}
div#uniform-catId.selector {
    /* visibility: hidden; */ /* Original, subcategory selector is hidden until a parent category is selected */
    visibility: visible; /* That's for you, you want this always visible for a search bar */
    width: 238px;
}
div#uniform-catId.selector span {
    width: 205px;
}
div#uniform-catId.selector select {
    min-width: 205px;
    width: 238px;
}

inc.search.php

Code: [Select]
<?php custItemForm::cust_category_two_selects(nullnull__('Select a category''modern')); ?>

Regards



teseo Thank you very much for this awesome mod.   I was just wondering if it is possible to have another drop down for the sub sub category which is dependent on the sub category. As I have categories that goes to the third level.

teseo

  • Hero Member
  • *****
  • Posts: 5686
Re: Category and Sub-category dropdown in search
« Reply #6 on: November 12, 2013, 10:57:04 pm »
Sorry buddy, still no experience with those sub-subcategories.

Regards

Essam

  • Newbie
  • *
  • Posts: 15
Re: Category and Sub-category dropdown in search
« Reply #7 on: April 22, 2014, 12:13:31 am »
@ teseo
Thank you very much..
I think that you still remember me and remember our discussion on this post:
http://forums.osclass.org/development/keeping-the-selected-region-and-perform-all-searches-relative-to-it/msg77602/#msg77602

 Anyway, related to current post, I am using the twitter theme and  I tried to use the code above but I got a blank page.

what I need is to show Category and Sub-category dropdown menu on side bar..

Could you please tell me exactly where and how I  can add the code on twitter theme?

Thank you in advance.

ievgen

  • Newbie
  • *
  • Posts: 37
  • KeepLine.org
Re: Category and Sub-category dropdown in search
« Reply #8 on: October 31, 2015, 11:15:41 pm »
hello, in search.php I replaced this script: <?php osc_categories_select('sCategory', null, __('Select a category', 'aurum')) ; ?> on this:<?php ItemForm::category_multiple_selects(null, null, __('Select a category', 'aurum')); ?>
But appearing only main categories sub-categories not picked.
u can see here->http://keeplane.com/index.php?page=search&sCategory=90
« Last Edit: November 01, 2015, 09:23:12 am by ievgen »