How to get Even / Odd id numbers from database table in codeigniter

I'm new to codeigniter and just folded into a query to solve a report for a crash project. Please help me the experts on Codeigniter.

  • I have a large database table and only want to display even or even data rows from that table to be filtered by the table. The field is named "sale_id". I tried this in PHPMyadmin in raw coding and it worked for me. But it cannot be applied in Codeigniter.

SELECT * FROM ospos_pak_sub_cat WHERE id %2 =0;

Worked for me in raw PHP coding. How can I use it in Codeigniter. I used the Where clause already in this query and now I want to add a new query. Existing Where below is a condition that works fine.

$this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');

It works and I tried the code below to get a solution that doesn't work and gets an error.

$this->db->where('sale_id %2'=> 0);

Getting error on this line. is talking -

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)

Please help me find solutions. Thank you in advance.

+3


source to share


4 answers


In your model, just write your query like this:

$this->db->select('*');
$this->db->from('ospos_pak_sub_cat');
$this->db->where('sale_id %2=', 0);
$query_result = $this->db->get();
$result = $query_result->result();

      



You missed the '=' in your code. Hope this works.

+5


source


I'm not sure, but I think it will work. Try this line $this->db->where('sale_id %2',0);



0


source


As you mentioned, you can add a new condition to the existing one where the condition is like below

$this->db->where('(sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].')" and ((sale_id % 2) = 0)');

      

0


source


in SQL, the% character is a wildcard and not a module that explains your error. you can use MOD function instead of http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_mod

so the resulting code will look like this:

    $this->db->where('MOD(sale_id ,2) => 0');

      

0


source







All Articles