0

I have a channel with 2 fields, "New Square Footage" and "Remodeled Square Footage". I'm displaying both of these numbers in my template. I want to be able to sort the entries by the total of both of those fields. I currently have a form that will sort based on one of those fields and it works fine using Dynamic Parameters http://ellislab.com/expressionengine/user-guide/modules/channel/dynamic_parameters.html. Any way to add these fields together? I'm not sure if it can be done through the template, or if there needs to be a hidden field in the Publish form that adds them together...Any ideas?

My current form

        <ul>
        <li>
        <label>Order By:</label>
        <select name="orderby">
            <option value="project_state">State</option>
            <option value="project_completion_date">Completion Date</option> 
            <option value="project_contract_amount">Contract Amount($)</option>
            <option value="project_new_sq_ft">Square Footage</option>
        </select>
        </li>
        <li>
        <label>Sort:</label>
        <select name="sort">
            <option value="asc">Ascending</option>
            <option value="desc">Descending</option>
        </select>
        </li>
        <li>
        <input type="submit" value="Go!" />
        </li>
    </ul>

"prject_new_sq_ft" is one of the fields, I need that to be the total of both fields.

1 Answer 1

1

Math add-on?

This one is free and has a lot of praise.

But I'm assuming you need the total on the page that receives the form, and as you may not be able to use the add-on as a parameter, it may not work. Worth a try.

Otherwise what about simply using JQuery/JavaScript on the form onChange event for either/both the fields, that automatically populates a hidden field with the combination of the 2 fields.

UPDATE: Another route...

Parent template uses an add-on (there are others) to get the POST or GET variables, you then pass those to the child embed template to be used in the channel:entries, which no longer uses dynamic parameters.

Parent Template:

{embed=template-group/child-template 
    orderby="{exp:server_var:server:post var="orderby"}"
    sort="{exp:server_var:server:post var="sort"}"
    combined="{exp:will_hunting:solve_pair}{$result={exp:server_var:server:post var="prject_new_sq_ft"}+{exp:server_var:server:post var="prject_remodel_sq_ft"}}{/exp:will_hunting:solve_pair}"}

Child Template:

{exp:channel:entries sort="{embed:sort}" orderby="{embed:orderby}" limit="{embed:combined}"}

Reading back through your question, I'm a little confused as to what "I want to be able to sort the entries by the total of both of those fields." actually means as you can't "sort" by a number, so in the example above I've just used it for limit to give you the idea and you can manipulate as you see fit.

I've used a different Math add-on, as looking into it MX Calculator won't allow the tags in the parameter, but this one looks as though it could work (obviously there are others).

It's doubtful that the code above will work without further tweaking, possibly adding parse="inward" to the server_var tags. But hopefully it'll give you the right direction.

1
  • I think since I'm sorting using dynamic parameters, and only passing the name of the field to sort by, adding the numbers in the form template wouldn't do anything. I think I need some kind of fieldtype in the CP that would add the numbers from the other 2 fields and record that sum...might be a custom job? Aug 1, 2013 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.