3
votes

i wanna ask about active record codeigniter is it possible to use IF statement in where active record?

$this->db->where("IF((month(a.create_date)) < month(now()) , d.approve_by, '') like $username" );

So, i want to get data approve_by but the create_date must be month before current month

i have try with mysql, and it can.

IF(month(a.create_date) < month(NOW()),b.approve_by,'') = 'someusername'

when i try to implement on active record, that not showing anything.

Thanks for help :)

2
Thanks for reply, but that just for 1, ('blabla',$blabla). for case, how to use a column from database? - Chandra Putra Wijaya
you want to fectch previous months records?? - Niranjan N Raju
yes, but now its done. i make new menu, so it divide by 2 query who running separately - Chandra Putra Wijaya

2 Answers

1
votes

from the Codeigniter Userguide

$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.

$this->db->where("IF((month(a.create_date)) < month(now()) , d.approve_by, '') like $username", NULL, FALSE);

0
votes

We can use the CASE of MYSQL as mentioned below.


  $this->db->select(
     '(CASE
            WHEN orderdetails.ProductID = 0 THEN dealmaster.deal_name
            WHEN orderdetails.DealID = 0 THEN products.name
        END) as product_name'
    );