3

I'm using CodeIgniter 3 to build a website. Now I have an issue with getting data from the database.

This is my Model:

public function Get($arr_condition = null)
{
     if (!is_null($arr_condition))
     {
        $this->db->where($arr_condition);
     }
     $query = $this->db->get('cus');
     if ($query->num_rows() > 0)
     {
        return $query->result_array();
     }
     return false;
}

In my Controller, I use an array ($arr_condition) to list all conditional expressions.

$arr_condition['Cus_team'] =  1;
$arr_condition['Cus_user != '] =  2;
$arr_condition['Cus_phone LIKE'] = 09900000;
$arr_condition['Cus_role >'] = 1;

$result = $this->M_cus_dal->get($arr_condition);

My code is working perfectly until I need a condition with where_in(), I tried:

$arr_condtion['Cus_id IN']= array('1,2,3');

But, It not work.

Please give me an ideal to resolve this issue.

1
  • 1
    Rarely does it make any sense to use LIKE phone number. Commented Sep 1, 2024 at 2:03

3 Answers 3

3

You can do something like this:

In controller pass your where_in condition with another parameter like this

$where_in = array('1,2,3');
$result = $this->M_cus_dal->Get($arr_condition,$where_in);

In model method get $where_in as second parameter as add it to where_in clause like this :

public function Get($arr_condition = null,$where_in = NULL)
{
     if(!is_null($arr_condition))
     {
        $this->db->where($arr_condition);
     }
     if(! empty($where_in))
     {
       $this->db->where_in('Cus_id',$where_in);
     }
     $query = $this->db->get('cus');
     if($query->num_rows()>0)
     {
        return $query->result_array();
     }
     return false;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks you @pradeep, I tried first way but the result in mysql is Cus_id = 'IN (1,2,3)'. So I will try the second way.
and what about the second way it is much better way to get desired result
should be like this $arr_condtion['Cus_id IN '] = '(1,2,3)'; check whether it is working or not but prefer the second way, pls let me know
Thank you @pradeep, I tried the second way and it work right. Now I dont have more time so I will optimaze my function base your ideal. Thank once more time!
0

If you use where_in then your ids should be in array

like this:

$where_in[] = 1;
$where_in[] = 2;
$where_in[] = 3;

OR

$where_in = array('1','2','3');


$this->db->where_in('Cus_id', $where_in);

Comments

-1

This task is quickly slipping into a messy, over-accomodating model method.

You already have limited WHERE condition syntax, one of which is poorly/incorrectly implementing a LIKE condition. As requirements grow, you'll either need to parse and separate the keys to perform diverse conditional operations or you'll need to continue build more and more parameters to the model method signature ($where, $like, $in, $notLike, $notIn, $orderBy, $limit, $select, $join`)

No, I recommend that you curb the urge to build a mega-utility method. Build what you need, as you need it, with the intention of keeping its purpose narrow and its parameters minimal.


If you MUST have the sought functionality as a single parameter, then you'll need to make conditional query builder calls.

Controller:

$conditions = [
    'Cus_team' => 1,
    'Cus_user !=' => 2,
    'Cus_phone LIKE' => 09900000,
    'Cus_role >' => 1,
    'Cus_id' => [1, 2, 3],
];

$result = $this->M_cus_dal->get($conditions);

Model:

public function get(array $conditions = []): array
{
    foreach ($conditions as $k => $v) {
        if (is_array($v)) {
            if (!$v) {
                continue; // avoid breaking built query
            }
            $this->db->where_in($k, $v);
        } elseif (str_ends_with($k, ' LIKE')) {
            $this->db->like(substr($k, 0, -5), $v);
        } else {
            $this->db->where($k, $v);
        }
    }
    return $this->db->get('cus')->result_array();
}

The model method will return an array containing zero or more subarrays.

I never return false from a method which ordinarily returns a 2d array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.