Filtering Query WHERE

Filtering Query WHERE

vicfebovicfebo Posts: 20Questions: 0Answers: 0
edited December 2012 in Editor
Hello,

I want to filter my Query Select. I want to Select only id_operador < 40
This is my code:

[code]
$out['operadores'] = $db
->select( 'operadores', 'id_operador as value, nombre_operador as label', array("id_operador" => "40"))
->fetchAll();
[/code]

But this, only filter the id_operador = 40. I want id_operador < 40.
How I do please??

Thank you!

Replies

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Use the `where` method. Note that you need to use `query` to build the query rather than `select` (as `select` is just a shortcut which returns a result).

    Allan
  • vicfebovicfebo Posts: 20Questions: 0Answers: 0
    [quote]allan said:
    Use the where method. Note that you need to use query to build the query rather than select (as select is just a shortcut which returns a result).[/quote]
  • vicfebovicfebo Posts: 20Questions: 0Answers: 0
    Is Any example for Where method please?

    I try this, but it´s not work:

    [code]
    $out['operadores'] = $db
    ->select( 'operadores', 'id_operador as value, nombre_operador as label')
    ->where( 'id_operador','40','<' )
    ->fetchAll();
    [/code]

    I saw that the method is this:

    [code]
    public function where ( $key=null, $value=null, $op='=' )
    [/code]


    Thank you again!
  • vicfebovicfebo Posts: 20Questions: 0Answers: 0
    Please, also I need order the results in ASC mode, it´s posible?

    Thanks
  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    [code]
    $db
    ->query( 'select', 'operadores' )
    ->fields( 'id_operador as value', 'nombre_operador as label' )
    ->where( 'id_operador','40','<' )
    ->exec()
    ->fetchAll();
    [/code]

    Ordering can be done with the `order` method: http://editor.datatables.net/docs/current/php/class-DataTables.Database.Query.html#_order

    Allan
  • vicfebovicfebo Posts: 20Questions: 0Answers: 0
    I put this:

    [code]
    if ( !isset($_POST['action']) ) {

    $out['operadores'] = $db
    ->query( 'select', 'operadores' )
    ->fields( 'id_operador as value', 'nombre_operador as label' )
    ->where( 'id_operador','40','<' )
    ->exec()
    ->fetchAll();
    }
    [/code]

    But not work, this is the error:

    Fatal error: Call to undefined method DataTables\Database\DriverMysqlQuery::fields() in php/table.competitive_prices.php on line 82


    Why?

    Thank you
  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Because I didn't read my own documentation - oops! `fields` is a method of rate Editor class, not the Query class. It should have been `get` - http://editor.datatables.net/docs/current/php/class-DataTables.Database.Query.html .

    Try this

    [code]
    $out['operadores'] = $db
    ->query( 'select', 'operadores' )
    ->get( 'id_operador as value, nombre_operador as label' )
    ->where( 'id_operador','40','<' )
    ->exec()
    ->fetchAll();
    }
    [/code]

    Allan
  • vicfebovicfebo Posts: 20Questions: 0Answers: 0
    Greattt !!!! Thank you very much!!!
  • MbdesignMbdesign Posts: 17Questions: 0Answers: 0
    i try to use order but dosn´t work?

    Call to undefined method DataTables\Editor::order()
  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    > DataTables\Editor::order()

    The Editor class doesn't have an `order` method: http://editor.datatables.net/docs/current/php/class-DataTables.Editor.html

    Do you perhaps what to do an `order` on a `Query` instance: http://editor.datatables.net/docs/current/php/class-DataTables.Database.Query.html#_order ?

    Allan
This discussion has been closed.