Search code examples
phpcodeignitercodeigniter-3

how to check the id available in the table column have prefix value in CodeIgniter


I'm using codeigniter for my project. I've store the member id in the member table. Member Id Pattern is "KZI22M8547". Last 4 digits are random, rest of the characters are generated depend upon the form inputs values. I want to check whether the new member id (contains random number for the last 4 digits) is available while creating the new members.

I'm using the following code in the model
$query = $this->db->get_where('members', array(
            'member_id' => $member_id
        ));
        if ($query->num_rows() > 0) {
            return true;
        } else {
            return false;
        }

Solution

  • $this->db->select('SUBSTRING(member_id, 7) as member_id', FALSE);
    $query = $this->db->get_where('members', array(
        'ch_id' => $ch_id
    ))->result();
    
    if (in_array($rand_no, array_column($query, 'member_id'))) {
        return true;
    } else {
        return false;
    }