4

I'm getting myself into a potential pickle here and wanting to know the best way to get apply filters on the current scenario:

I have a central business section which is getting populated via an API (just a little background info) and I have a second section drugs which has a Entries field that contains a link to business.

I've designed a table that pulls in all this info that looks a little something like this:

company drug table I've built out the table like this:

{# DEFAULTS #}
{% set queryParams = {
    section: 'business',
    order: 'title asc',
    limit: 30
} %}
{% for entry in craft.entries(queryParams) %}
    <div class="row">
        <div class="td td--comapny"><small>{{entry.title}}</small> <b>{{entry.business_name}}</b></div>
        <div class="td td--price">
        {{entry.business_price}}
        {% if "+" in entry.business_change %}
            <small style="color:#0c0">{{entry.business_change}} {{entry.business_percentChange}}</small>
        {% else %}
            <small style="color:#c00">{{entry.business_change}} {{entry.business_percentChange}}</small>
        {% endif %}
        </div>
        <div class="td td--market_cap">${{entry.business_market_cap}}</div>
        <div class="td td--shares">{{entry.business_number_of_shares|number}}</div>
        <div class="td td--short_ratio">{{entry.business_shortRatio}}</div>
        <div class="td td--short_ratio">{{entry.dateUpdated}}</div>
        <br style="clear:both" />
        <table style="width: 100%;">
            <thead>
                <tr>
                    <th style="text-align: left;width:20%">Drug</th>
                    <th style="text-align: left;width:30%">Indication</th>
                    <th style="text-align: left;width:20%">Stage</th>
                    <th style="text-align: left;width:30%">News</th>
                </tr>
            </thead>
            <tbody>
            {% set relatedDrugs = craft.entries.section('drug').relatedTo(entry) %}
            {% for drug in relatedDrugs %}
            <tr>
                <td>{{ drug.title }}</td>
                <td>{{ drug.drug_indication}}</td>
                <td>{{ drug.drug_stage.label}}</td>
                <td><a href="{{ drug.drug_press_link}}">{{ drug.drug_note}}</a></td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
{% endfor %}

Now – the pickle is the filtering side of things: I'm wanting to filter on the drug stage, AND to make matters even more exciting this is a multi-select filter which I've done like this:

<fieldset>
    <legend>Stage</legend>
    {% set stage = craft.fields.getFieldByHandle('drug_stage') %}
    {% for stage in stage.settings.options %}
        {% if loop.first %}<select name="stage[]" multiple>{% endif %}
          <option value="{{stage.value}}">{{stage.label}}</option>
        {% if loop.last %}</select>{% endif %}
    {% endfor %}
</fieldset>

I'm not really sure how to enable multiple params of the same name so I've called it stage[] which handily returns an array. The problem I'm having now is building that new param filter query... I got as far as this and stared blankly at the screen for 10 minutes (I'm no programmer...)

{% set stage = craft.request.getParam('stage') %}
{% if stage is not empty %}
    {# NO IDEA #}
{% endif %}

I really have no idea where to start. Any pointers or help would be fantastic!

Matt Stein
  • 4,006
  • 3
  • 26
  • 57
Darren Wood
  • 117
  • 6
  • Hey @DarrenWood, could you accept Dan's answer if it was helpful or post your own answer if you solved it differently? – Matt Stein May 09 '17 at 21:56

1 Answers1

1

This should take care of it:

{% set stage = craft.request.getParam('stage') %}

{# check if stage is an array #}
{% if stage is iterable %}

    {% for stageItem in stage %}

        {# make sure stageItem has content #}
        {% if stageItem is not empty %}

            {{ stageItem }}
            {# would output the selected stage.value #}

        {% endif %}

    {% endfor %}

{% endif %}
Dan Ott
  • 306
  • 1
  • 8
  • I edited this to make it a little stronger. We want to test the array to make sure it's an array, but it's each item that we want to make sure is not empty. – Dan Ott Jun 16 '16 at 13:38