0

I'm using two search fields, each searching a different category group, and creating an inclusive category search by combining those fields. The form code looks like this:

<form id="directory-search" method="post" action="/directory/search">
        {exp:super_search:search}
        <fieldset class="search">
            <div class="center">
                <h1>Directory</h1>
                <a class="btn directory open-collapsed mobile-only" href="#">Search</a>
                <ul class="search-row">
                    <li>
                        <label for="channel">What?</label>
                        <select class="replace-hfirst" name="category[]">
                            <option value="">- choose vendor type -</option>
                            {exp:channel:categories category_group="10" style="linear" show_empty="no"}
                            <option value="{category_url_title}&&" data-selectedtext="{category_name}" {if super_search_category_{category_url_title}}selected="selected"{/if}>{if parent_id != '0'}&nbsp;&nbsp;&nbsp;&nbsp;{/if}{category_name}</option>
                            {/exp:channel:categories}
                        </select>
                    </li>
                    <li>
                        <label for="category">Where?</label>
                        <select class="replace-hfirst" name="category[]">
                            <option value="">- choose location -</option>
                            <option value="" data-selectedtext="All Locations" {if super_search_category == ""}selected="selected"{/if}>All Locations</option>
                            {exp:channel:categories category_group="2" style="linear" show_empty="no"}
                            <option value="{category_url_title}" data-selectedtext="{category_name}" {if super_search_category_{category_url_title}}selected="selected"{/if}>{if parent_id != '0'}&nbsp;&nbsp;&nbsp;&nbsp;{/if}{category_name}</option>
                            {/exp:channel:categories}
                        </select>
                    </li>
                    <li>
                        <label for="keywords">Search</label>
                        <input type="text" class="search" name="keywords" value="{super_search_keywords}" placeholder="Optional Keyword" />
                    </li>
                    <li class="button">
                        <a class="btn directory submit" href="#">Search</a>
                    </li>
                </ul>
            </div><!-- end .center -->
        </fieldset>
        {/exp:super_search:search}
    </form>

And the resulting search string could look like this: search&category=auckland+photo

"auckland" is a category in one group and "photo" is a category in the second group.

Now I need to find a way to add "selected" to the select option for each category so the searched category is highlighted.

Normally I would do something like:

{if super_search_category == "{category_url_title}"}selected="selected"{/if}

...inside the select option tag. But that doesn't work now that there are multiple categories combined in the string.

The other logical thing would be something like this:

{if super_search_category_{category_url_title}}selected="selected"{/if}

Which , according to the docs sounds like it might be the right format to work with multiple inclusive categories, but that doesn't work - probably a parsing issue trying to add in the category_url_title.

So how do I go about testing to see if an inclusive search string contains each category_url_title so I can select the right option in the select boxes?

2 Answers 2

1

When there are multiple categories, {super_search_category} will display the entire category string.

In this example, if you have search&category=auckland+photo, then {super_search_category} will parse to:

auckland+photo

or, in some setups/EE installations,

auckland photo

To use the individual selections for something, you'll need to break up this string. You can do this with PHP.

First, turn on PHP for your template and set it to Output.

Then, break up the string into an array of individual categories:

<?php $cats = explode(" ", '{super_search_category}'); ?>

(Note: in some cases, you might need to change " " to "+")

Next, loop through each category in the array and use a conditional to check if there's a match with your {category_url_title}:

<?php foreach($cats as $cat) :?>
    <?php if($cat == '{category_url_title}') :?>
        selected="selected"
    <?php endif ?>
<?php endforeach ?>

The other approach, using {if super_search_category_{category_url_title}}selected="selected"{/if}, was close, but there is indeed a parsing issue. If you want to go down that strategy path, you might need embeds and pass in the {category_url_title} variable, so that {embed:my_variable} parses before the conditional. However, you'll have a much bigger performance hit. The only other option would be to hand-code the values for each {category_url_title} in {super_search_category_my_category}, but that's not very dynamic.

3
  • Thanks this looks very promising. Can you please elaborate on what you mean by "in some cases"? When exactly would it return "auckland+photo" and when "auckland photo". Just want to understand what that distinction is. Commented Jan 25, 2014 at 3:21
  • "In some cases" should really be "in some server setups/EE installations". It's not something that's random on an installation. I edited the answer above. You'll know if a "+" or " " displays by using {super_search_category} in your template. In fact, "auckland photo" is probably what you'll see, as that is what I usually observe.
    – Solspace
    Commented Jan 25, 2014 at 4:37
  • This worked great but I have a follow up: How can I return the category name rather than using it as a conditional? The case here is that I want to write out a little search summary like "Searching for category1_name in category2_name with keywords {super_search_keywords}". Obviously I can only grab the category_url_titles for the two searched categories so I would need a way to then convert that to category_name. Sound possible or am I dreaming? Commented Jan 25, 2014 at 8:04
0

I've found an answer to my own follow up question after Solspace's answer which I'll pot here for others.

To convert multiple Super Search inclusive categories into category names:

I'm using a combination of an embedded template, channel:categories tag, and CE String (http://devot-ee.com/add-ons/ce-string).

In my results page I have this:

{embed="directory/.ss-catname" group="10" cats="{super_search_category}"} 

and this

{embed="directory/.ss-catname" group="2" cats="{super_search_category}"} 

and then in that embed I have this:

{exp:channel:categories category_group="{embed:group}" style="linear"}
{if "{exp:ce_str:ing strpos='{category_url_title}'}{embed:cats}{/exp:ce_str:ing}" != "-1"}{category_name}{/if}
{/exp:channel:categories}
{if {embed:group} == "2" && "{exp:ce_str:ing str_word_count}{embed:cats}{/exp:ce_str:ing}" == "1"}All Locations{/if}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.