Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Display multiple terms

Tagged: 

Viewing 9 posts - 1 through 9 (of 9 total)
  • Kim
    #171604

    I’ve been crunching through the forum, but not seeing an answer.

    – – – – – – – –

    I’m using the following code to display a list of current terms for the filtered search:

    </php
    
    global $searchandfilter;
     
    $sf_current_query = $searchandfilter->get(99801)->current_query()->get_array();
    echo '<strong>Filters</strong>';
    
    if ( empty($sf_current_query ) ) {
      echo ' &bull; ' . "All" . "<br />";
    } else {
      foreach($sf_current_query as $key) {
      echo ' &bull; ' . $key['active_terms'][0]['name'] . ' ';
    }
    }
    echo "<br /><br />";
    
    ?>

    – – – – – – –

    My filter includes dropdowns and checkboxes. For example:

    Market (dropdown)
    Florida

    Cuisines (checkboxes)
    [ ] American
    [X] Caribbean
    [ ] Italian
    [X] Mexican

    – – – – – – –

    The code works and the list displays, but it only shows one of the selected checkbox terms…

    Filters • Florida • Mexican

    Instead of…

    Filters • Florida • Caribbean • Mexican
    OR.. Filters • Florida • Caribbean, Mexican

    I suspect that an array is required, but not sure what/where/how.

    Please help this newbie out.

    Thanks!

    Trevor Moderator
    #171651

    You are correct. You need to examine the data structure of the filter array and then, where multiple values are permitted, use a loop to output them.

    You can print the filter array to screen with code like this:

    $sf_current_query = $searchandfilter->get(1446)->current_query();
    echo '<pre>';
    print_r($sf_current_query->get_array());
    echo '</pre>';

    But then it is a matter of coding it in PHP. I cannot see an example code snippet on our forum though. I would have thought it possible to replace echo with explode where you are handling an array?

    Kim
    #171721

    I hear you, but that’s a class beyond my current grade.
    Anything you can help with, or…?

    Kim
    #171733

    Okay, good news, I’m making progress…

    This works for the array:

    <?php 
    
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(99801)->current_query();
    
    if ( empty($sf_current_query ) ) {
      echo '<strong>Filters</strong>' . ' &bull; ' . "All" . "<br /><br />";
    } 
      else {
        $args = array(
        "str"  => '%2$s', 
        "delim"  => array(", ", " - "), 
        "field_delim"  => ', ', 
        "show_all_if_empty"  => false 
      );
      echo '<strong>Filters</strong>' . ' &bull; ' . $sf_current_query->get_fields_html(array("_sft_cuisine", "_sft_name"), $args);
      }
    	
      echo '<br />';
    ?>

    Now I need to pull in the $key[‘active_terms’][0][‘name’] from the original code (it will display before the array).

    That will give me what I’m after…
    Filters • Florida • Caribbean, Mexican

    How do I combine it?

    Kim
    #171735

    Actually, $key[‘active_terms’][0][‘name’] pulls from both the dropdown field (“Market”) and the array (“Cuisines”), so that code wouldn’t be correct. I need the dropdown selection followed by the array.

    Trevor Moderator
    #171829
    This reply has been marked as private.
    Kim
    #171852
    This reply has been marked as private.
    Trevor Moderator
    #171854
    This reply has been marked as private.
    Kim
    #171986

    Trevor, thanks again for spending time with me today. I really appreciated it.

    Good news, I finally figured it out!

    Here’s the happy code:

    <?php 
    
    	global $searchandfilter;
    	
    	$args = array(
    		"str" 					=> '%2$s', 
    		"delim" 				=> array(", ", " - "), 
    		"field_delim"			=> ', ', 
    		"show_all_if_empty"		=> false 
    	);
     
    $sf_current_query = $searchandfilter->get(99801)->current_query()->get_array();
    $sf_current_style_query = $searchandfilter->get(99801)->current_query()->get_fields_html(array("_sft_style", "_sft_name"),$args);
    $sf_current_cuisine_query = $searchandfilter->get(99801)->current_query()->get_fields_html(array("_sft_cuisine", "_sft_name"),$args);
    	
    echo '<strong>Filters</strong>';
    
    if ( empty($sf_current_query ) ) {
      echo ' &bull; ' . "All" . "<br /><br />";
    } else {
    
      echo ' &bull; ' . $sf_current_query['_sft_category']['active_terms'][0]['name'] . ' &bull; ' . $sf_current_query['_sfm_wpcf-location-city']['active_terms'][0]['name'] . ' &bull; ' . $sf_current_style_query . ' &bull; ' . $sf_current_cuisine_query;
    	echo "<br /><br />";
    }
    ?>

    Cheers!

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Display multiple terms’ is closed to new replies.