Reputation: 317
I need to delete multiple rows and then reordering the next database records. ELDI_Order it's not an autoincrement.
public function delete_one_record($id)
{
$this->db->select('ELDI_Order');
$this->db->where("ELDI_Id", $id);
$eldi_order = $this->db->get('elem_diccio')->row()->ELDI_Order
// Delete that record
$this->db->delete('elementos_diccionario', array('ELDI_Id' => $id));
// Reorder ELDI_Order for the rest of the records
$this->db->set('ELDI_Order', 'ELDI_Order-1', FALSE);
$this->db->where('ELDI_Order >', $eldi_order);
$this->db->update('elem_diccio');
}
function eliminar_varios_elementos_diccionario($ids)
{
$this->db->where_in('ELDI_Id', explode(",", $ids));
$this->db->delete('elem_diccion');
// Code for reordering
}
For example, if I delete rows 2 and 8 (the 4th) I want to substract 1 to the column ELDI_Order of prueba_3 because it moved "1 place", and 2 from prueba_5 because it "moved" 2 places because I deleted 2 records.
Thanks for your help and time!
Upvotes: 0
Views: 401
Reputation: 9265
Just use your delete_one_record
function and a foreach. I can't see a way to inefficiently reorder multiple items at a time - so its not too bad to do them individually.
function eliminar_varios_elementos_diccionario($ids)
{
if (!is_array($ids)) {
$ids = explode(",", $ids);
}
foreach ($ids as $id) {
$this->delete_one_record(intval($id)); // assummed function is in same model
}
}
Suggestion: think about using transactions.
Upvotes: 1