Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Creating dynamic queries based upon form inputs
  • Hello,

    I'm new to object orientated PHP and I've had a look through this forum and I currently can't find anything to help yet.

    I have a model called Jobs, and I have a page that I'm designing to be a query builder to enable you to sort through Jobs. The jobs have some fields that are based upon select box inputs (e.g. Status, Stage, Size etc etc)

    It displays the Jobs currently in a table (the standard scaffolded view) and I can filter them based upon the input of one form field (a select box that selects the Stage to filter by) The user selects a Stage from the dropdown and then the form posts to itself and the query is run on the controller.

    I would like to be able to filter the 'table' of results by more than one field. Each column header will have a tickbox (to enable sorting by that field) then a dropdown with the results to filter by.

    Is there any way in being able to dynamically string together 'where' queries on the fly.

    Sort of an isset ( $tickboxvariable1) then set a another variable to be that select box value, then add another  ->where($var, $var2) etc to the query().

    Any help would be appreciated!

  • HarroHarro
    Accepted Answer
    Just do that, that's the beauty of objects. Assuming ORM:

    $query = Model_Something::query();
    Input::post('tickvar1', false) and $query->where('var1', Input::post('var1', null));
    Input::post('tickvar2', false) and $query->where('var2', Input::post('var2', null));
    $result = $query->get();
  • Thank you very much for this, it has worked a treat! I can now filter based upon many form inputs.

    Many thanks again!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion