Search code examples
phpcodeigniteractive-record-query

How do I delete a ci active record from db that is isolated in a foreach loop?


This is the function in the CI model:

public function libraryClean() {
    $killCount=0;
    $rows=$this->db->get('mz')->result();
    foreach ($rows as $row) {
        $rID=$row->rID;
        $title=$row->title;
        $char=$title[0];
        if (ctype_lower($char)) {
            $killCount++;
            // delete $row code goes here
        } 
    }
    return $killCount;
}

I've unsuccessfully tried various approaches. Thanks in advance.


Solution

  • You would just use an active record delete query.

    $this->db->delete('mz', array('id' => $rID)); 
    

    Being inside the foreach loop doesn't make the delete any more or less special.