ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Dynamic parameters: order and orderby in one dropdown?

October 31, 2012 11:35am

Subscribe [2]
  • #1 / Oct 31, 2012 11:35am

    For my first ExpressionEngine project I’ve found the documentation and this forum to be very helpful and working with EE has been a blast so far, but I’ve now run into something I can’t quite figure out:

    I’m looking to create a list of entries that is dynamically filterable and sortable, as is done in the documentation here. But I’m looking to make one selectbox (dropdown) which handles both the field that is used for sorting as well as the direction of the sorting, at the same time. It would have options like “cheapest first” (sort by price, descending) and “newest first” (sort by date, descending), “sort by brand” (ascending, alphabetically) and a few more. Since the options in the dropdown can only refer to one dynamic parameter at the same time, how would I go about setting this up? Would I need to set hidden input fields using javascript when the selectbox is changed, or is there a more elegant solution?

    Edit: Seems like I may have overestimated the power of the channel entries tag. As cool as it is, there’s a few features I need that most likely cannot be achieved with dynamic parameters alone. Namely the feature mentioned above, as well as filtering by a price range (min - max). I’m now thinking the query module might be able to help me here, but the examples in the documentation are a little basic compared to what I’m trying to achieve. Seems like I need the exp_channel_titles and exp_channel_data tables, and put my filters in the WHERE part of the query. An example of something like this would be very much appreciated.

  • #2 / Nov 02, 2012 12:54pm

    Dan Decker

    7338 posts

    Hi Geert van der Heide,

    You’ve go the right tables!

    You need to find the field_id_x column that matches your price field in the channel.

    {exp:query sql=' SELECT entry_id FROM exp_channel_data WHERE field_id_5 > low-price AND field_id_5 < high-price'}
       {embed='template-group/template' my_entry='{entry_id}'}
    {/exp:query}

    Then you have your embed template:

    {exp:channel:entries channel="products" entry_id="{embed:my_id}"}
       ...
    {/exp:channel:entries}

    You could use the dropdowns and pass the ranges and other parameters via segment variables to make it dynamic.

    Cheers,

  • #3 / Nov 02, 2012 1:30pm

    Thanks Dan, that helps a lot. It does also lead me to another question: If I set my form to method=“post”, no URL segments are created from the post data. I suppose the filtering and sorting information would need to be in the URL as a bunch of segments? Is there an easy way to make this happen?

  • #4 / Nov 04, 2012 12:42pm

    I think I’ve made some progress. Got a few questions left, they’re in bold.

    I’m now posting the “filter and order” form to a php file which redirects back to the original listing template but with the post variables as url segments:

    <?php
      $host     = $_SERVER['HTTP_HOST'];
      $category = (isset($_POST['category'])) ? $_POST['category'] : "all";
      $minprice = (isset($_POST['minprice'])) ? $_POST['minprice'] : 0;
      $maxprice = (isset($_POST['maxprice'])) ? $_POST['maxprice'] : "all";
      $orderby  = (isset($_POST['orderby'])) ? $_POST['orderby'] : "date";
      $order    = ($orderby == "date") ? "desc" : "asc";
      
      header("Location: http://" . $host . "/campers/" . $category . "/" . $minprice . "/" . $maxprice . "/" . $orderby . "/" . $order);
    ?>

    This allows me to use the url segments in the template, which is working. Is this a smart thing to do in my situation?

    After that, I’ve set up this code for listing the filtered entries:

    {exp:query sql="SELECT entry_id FROM exp_channel_data WHERE field_id_11>='{segment_3}' AND field_id_11<='{segment_4}'"}
      <li>{entry_id}</li>
      {if no_results}<li>No results.</li>{/if}
    {/exp:query}

    Which is now also working. Going to add the category filter and the sorting to the query next. I’ll update this post as I go along.

    I’m a bit worried about performance though. If I follow your example, I gather EE does a query for the list, then gets the embedded template, and inside that it does another query for every result. Will this hurt performance a lot?

    Edit: I had some more questions here before, which I ended up answering myself. @Dan: Your example has two faulty closing tags (exp:query and exp:channel:entries) 😊

  • #5 / Nov 06, 2012 2:35pm

    Dan Decker

    7338 posts

    Is this a smart thing to do in my situation?

    I don’t see anything wrong with this process.

    Will this hurt performance a lot?

    Good of you to be aware of that. It can if there are a lot of results. Use the template debugger to keep an eye on it.

    If it gets to be a burden, you could probably move your PHP into a plug-in and see a significant improvement.

    @Dan: Your example has two faulty closing tags (exp:query and exp:channel:entries) 😊

    Nuh uh… 😉 Thanks for the proof-read!

    Cheers,

  • #6 / Nov 10, 2012 2:01pm

    After trying a few solutions for the filtering and sorting I need, I ended up using a little more php and a little less EE. Now using query string parameters which are passed into the EE query module. PHP is parsed on input in the index template, and on output for the embedded template. I’ve made a Gist of it for those who are interested: https://gist.github.com/4052119

    Might end up improving and bugfixing it more later, but it’s working well for now. Thanks all, for pointing me in the right direction.

    This topic may now be closed 😊

  • #7 / Nov 12, 2012 1:28pm

    Shane Eckert

    7174 posts

    Hey there Geert,

    I am glad that Dan was able to help!

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases