All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Search Form Filter by Year Using entry_date

Stoyan Vasilev 9 Sep 2013 02:14 question, complete

Hi,

I am trying to setup a search form which has an html select/option dropdown with a year filter. I read that whatever I am doing has to work with the start_on and stop_before parameters.

My code will look something like this:

<code> 
<select name="">
<option value="">2012</option>
<option value="">2011</option>
<option value="">2010</option>
<option value="">2009</option>
</select>


The part that I do no understand is how to setup each option to work with both start_on and stop_before (in order for the two to cover exactly one calendar year). A code example of setting this up would be super helpful.

Thanks.

Replies

  1. Low 9 Sep 2013 07:32

    Just think of how you would get entries for a single year with the native channel:entries tag. You'd use the year="" parameter. You can use that parameter as an input field as well, just like any other parameter. So, you could simply use this:

    <select name="year"> 
    <option>2013</option>
    <option>2012</option>
    <option>2011</option>
    </select>

  2. Stoyan Vasilev 9 Sep 2013 18:25

    Thank you! I did not realize the "year" parameter existed in the native tag. I should really read through the whole list of parameters.

    The year filter works now and I have two other questions:

    1) How do I preselect the year (in the years drodown) from the search query after the query is executed?

    2) How do I preselect the collection (in the collections dropdown) from the search query after the query is executed?

    My code looks like this: http://pastie.org/private/ravrubpjdl2...

  3. Low 10 Sep 2013 07:51

    You need to add the query-parameter to the Form tag, just like the Results tag.

    Then you can use conditionals like this:

    {if collection_id == low_search_collection_id} selected="selected"{/if}


    and this:

    {if 2009 == low_search_year} selected="selected"{/if}


    Also, since you're only letting the user choose a single collection, you can omit the square brackets in the <select> element name.

  4. Stoyan Vasilev 10 Sep 2013 16:56

    Hey,

    I added the query parameter to the Form tag and removed the square brackets from the name attribute of the select element. The low_search_year value is outputted correctly and I implemented the year conditional statement successfully.

    low_search_collection_id however does not render a value. Example, when I put this in my template:

    <code> 
    {collections show="not private_collection"}
    <option value="{collection_id}" data-test="{low_search_collection_id}">{collection_label}</option>
    {/collections}


    I get this in the outputted markup:
    <code> 
    <option value="3" data-test="">Columns</option>
    <option value="2" data-test="">Features</option>


    What should I do?

    Thanks.

  5. Low 10 Sep 2013 16:59

    Ah right, I should read my own documentation. :)

    Check out the vars available in the {collections} var pair.

    You can use {collection_is_active} var in your conditionals.

    Also, you can leave out the show="not private_collection" bit, as that's just an example.

  6. Stoyan Vasilev 10 Sep 2013 17:21

    Kool. That did it. Thank you!