WordPress.org

Forums

Participants Database
[resolved] Complex search with checkboxes (7 posts)

  1. webmoxie
    Member
    Posted 2 years ago #

    I have a search form that intakes a drop down, and 7 checkboxes. I want to use the search as a filter, so that anything that meets all of the boxes checked will be return. I've been using the do_shortcode, but the problem is that unchecked checkboxes return empty, and then I only see results for participants who DON'T have those things checked, instead of just ignoring a field if unchecked. Basically, I want the filter="stuff" to only include arguments when the box has been checked, and not include an empty argument like field=&nextfield=& etc. Suggestions? Maybe something with php making if(empty) arguments?

    http://wordpress.org/extend/plugins/participants-database/

  2. webmoxie
    Member
    Posted 2 years ago #

    My other thought is that the unchecked field could return some kind of wildcard, so that if unchecked, that field's value can be anything. Is there a way I could do this?

  3. xnau
    Member
    Plugin Author

    Posted 2 years ago #

    Well, I am mostly guessing here, not seeing the rest of what you're doing, but it wound't be hard to only add the values to the filter if it was looking for a match.

    Instead of this:

    <?php echo do_shortcode('[pdb_list filter="field1='.$_GET['field1'].'&field2='.$_GET['field2'].'"]') ?>

    You'd do something like:

    <?php
    $filter = array();
    foreach ($_GET as $name => $value) {
       if (!empty($value) $filter[$name] = $value;
    }
    $filterstring = '';
    if (!empty($filter) {
       $filterstring = 'filter=';
       foreach ($filter as $name => $value) {
          $filterstring .= $name . '=' . $value . '&';
       }
    }
    echo do_shortcode('[pdb_list filter="' . rtrim($filterstring, '&') . '"]')
    ?>

    You might have to add some code to keep irrelevant GET variable from being added, but you get the idea.

  4. webmoxie
    Member
    Posted 2 years ago #

    This (as of yet) isn't working for me - it doesn't filter anything at all. Do you have any more advice for using the code you supplied?

    My other thought was, could I have unchecked boxes return some kind of wildcard? As in, if Field1 is checked and Field2 is unchecked, it would return filter="field1=value&field2=*", the * meaning anything, so that it would return all entries where field1 equaled the value, and field2 equaled anything at all (even a null value).

  5. xnau
    Member
    Plugin Author

    Posted 2 years ago #

    The way to filter out empty fields is to use "field!" with no value.

    The problem with the code I gave you is I'm only giving you the receiving end of it...you have to supply the search terms in the URL, which is done using a form with the GET method. I thought you already had that going. Set up your search form using the GET method with the list page as the action. Then you'll have two fields in the search form, "field1" and "field2". When the user searches, the resulting URL will have the search terms in it and that's where the code I gave you comes in.

    For instance if they search on one field and not on the other, the resulting URL will include the a GET string like this: ?field1=stuff&field2= So, that gives you your two search terms.

    I'm giving you a new version of the code that translates that into the filter string for the shortcode: filter="field1=stuff&field2!" This code has to go into the WP template displaying the list...not the plugin template. The search form can go either in the WP template or the plugin list template.

    <?php
    $filterstring = '';
    if (!empty($_GET) {
       $filterstring = '';
       foreach ($_GET as $name => $value) {
          $op = empty($value) ? '!' : '=';
          $filterstring .= $name . $op . $value . '&';
       }
    }
    echo do_shortcode('[pdb_list filter="' . rtrim($filterstring, '&') . '"]')
    ?>
  6. webmoxie
    Member
    Posted 2 years ago #

    I did have the search form, but this new code for the page template works great. Thank you so much for all of your (incredibly speedy) help!!!

  7. timd_g
    Member
    Posted 1 year ago #

    Hi webmoxie

    I'm looking for a similar solution: to give the user some checkboxes so they can search for items in the selected categories. Can you give me some advice how to get started with this? I already made the page templates.

    Thanks in advance!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Participants Database
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.