Search code examples
codeigniterwhere-clausegrocery-crud

How to combine and & or operators in grocery crud


I am using codeigniter and grocerycrud, I want to set a table with grocerycrud in this way:

SELECT * FROM `dashboard`.`medidas_ludlum`
where FK_ludlum='190.26.88.131'
and date>= '2014-03-04 09:40:00'
and date<= '2014-03-05 09:40:00'
and (error_code=1 or audio_status=1);

I tried to do that in this way:

  $uno='1';
  $crud2 = new grocery_CRUD();
  $crud2->set_theme('datatables');
  $crud2->where('FK_ludlum',$ludlum_id);
  $crud2->where('date>=',$fechainicio);$crud2->where('date<=',$fechafin);
  $crud2->where('error_code =',$uno);
  $crud2->or_where('audio_status =',$uno);
  $crud2->set_table('medidas_ludlum');
  $crud2->columns('measurement', 'fecha', 'audio_status', 'high_alarm_status', 'low_alarm_status','over_range_status','monitor_status','error_code');
  $crud2->set_language("spanish");
  $crud2->unset_add();$crud2->unset_delete();$crud2->unset_edit();$crud2->unset_read();
  $data['crud2'] = $crud2->render();

However it's not giving the right results, for example I am getting rows that have a date out of the range, is there a way to get that CRUD set up?


Solution

  • Grocery curd is also using codeigniter's active record so you can write your where() with grouped conditions as below,no need to use or_where() function

    $crud2->where('(error_code ='.$uno.' OR audio_status ='.$uno.')',null,FALSE);
    

    or

    $crud2->where('(error_code =1 OR audio_status =1)',null,FALSE);
    

    The problem in your query is when you use or_where() function it produces where clause as and error_code=1 or audio_status=1 and using approach that i have provide will give you the where clause as and (error_code=1 or audio_status=1) a grouped condition

    API and Functions list