Trong controller :
public function Update11to10() { $data = $this->companies_model->Update11to10(); var_dump($data); }
Trong models
public function Update11to10() { $this->db->select('*'); $this->db->from('companies'); $q =$this->db->get(); if ($q->num_rows() > 0) { foreach (($q->result()) as $row) { $phone = $row->phone; $phone = trim($phone); $phone = preg_replace('/[^0-9]/', '', $phone); // xóa đầu 84 thành số 0 $str = substr($phone, 0, 2); if ($str == '84') { $phone = 'hien123'.$phone; $phone = str_replace('hien12384', '0', $phone); } $dodai_phone= strlen($phone); // xóa các số phone ko đúng if ($dodai_phone == 11 || $dodai_phone == 10 ) { if ($dodai_phone == 10) { $this->db->where('id', $row->id); $this->db->update('companies',['phone'=>$phone]); } if ($dodai_phone == 11) { $str_trong = substr($phone, 0, 4); $phone = 'hien'.$phone; if ($str_trong == '0127') { $phone = str_replace('hien0127','081',$phone); } if ($str_trong == '0162') { $phone = str_replace('hien0162','032',$phone); } if ($str_trong == '0163') { $phone = str_replace('hien0163','033',$phone); } if ($str_trong == '0164') { $phone = str_replace('hien0164','034',$phone); } if ($str_trong == '0165') { $phone = str_replace('hien0165','035',$phone); } if ($str_trong == '0166') { $phone = str_replace('hien0166','036',$phone); } if ($str_trong == '0167') { $phone = str_replace('hien0167','037',$phone); } if ($str_trong == '0168') { $phone = str_replace('hien0168','038',$phone); } if ($str_trong == '0169') { $phone = str_replace('hien0169','039',$phone); } if ($str_trong == '0120') { $phone = str_replace('hien0120','070',$phone); } if ($str_trong == '0121') { $phone = str_replace('hien0121','079',$phone); } if ($str_trong == '0122') { $phone = str_replace('hien0122','077',$phone); } if ($str_trong == '0123') { $phone = str_replace('hien0123','083',$phone); } if ($str_trong == '0124') { $phone = str_replace('hien0124','084',$phone); } if ($str_trong == '0125') { $phone = str_replace('hien0125','085',$phone); } if ($str_trong == '0126') { $phone = str_replace('hien0126','076',$phone); } if ($str_trong == '0128') { $phone = str_replace('hien0128','078',$phone); } if ($str_trong == '0129') { $phone = str_replace('hien0129','082',$phone); } if ($str_trong == '0186') { $phone = str_replace('hien0186','056',$phone); } if ($str_trong == '0188') { $phone = str_replace('hien0188','058',$phone); } $this->db->where('id', $row->id); $this->db->update('companies',['phone'=>$phone]); } } else { $this->db->delete('companies', array('id' => $row->id)); } } return $data; } }