CodeIgniter Active Record Delete Multiple Records At Once


This is one of my controller functions. It grabs all the rows from a database table called 'users' and puts it into an array. It then loads a view called 'deleteuser' with the array data passed through.

function deleteuser(){ $this->load->model('users'); $employees['staff'] = $this->users->getStaff(); $this->load->view('deleteuser', $employees); }

In the deleteuser view, there's a checkbox type input generated from the array. The form action calls on a function called remove.

<form action="remove" method = "post"> <?php foreach($staff as $row): ?> <div class="checkbox"> <label> <input type="checkbox" name="delete[]" value="<?php echo $row->id ?>" /> <?php echo $row->lastName . ", " . $row->firstName; ?>
</label> <?php endforeach; ?>

<input type="submit" name = "remove" value = "Delete" class = "btn btn-danger btn-lg pull-left" /> </div>

The remove function grabs the delete array from the input and passes it to a model function called deleteUser.

function remove(){ $data = $this->input->post('delete'); $this->users->deleteUser($data); }

Now this is where I'm running into some trouble. Below is the model function for deleteUser, but it's giving me an undefined offset: 1 error. Any help would be appreciated.

function deleteUser($data) { if ($data) { for ($i = 0; $i <= count($data); $i++) { $this->db->where('id', $data[$i]); $this->db->delete('users'); } } }

Though answer by @DamienPirsy is correct in addressing your problem, I would suggest an alternate approach. I would delete all records at once, rather than in a loop. This will minimize your number of queries against the DB.

function deleteUser($data) { if (!empty($data)) { $this->db->where_in('id', $data); $this->db->delete('users'); } }

Remove the equal = sign, should be

for ($i = 0; $i<count($data); $i++)

you're fetching 1 element out of bounds. i.e, if count($data) is 4, you're looping: 0 1 2 3 4 ("count minor or equal to 4"), which is five elements indeed :)



  • search the database based on the value selected from database
  • Storyboard iOS MBProgressHUD
  • Can't perform CORS request using Angularjs
  • How to implement JQuery confirm dialog with JSF
  • VS2010: Ctrl-PgUp / -PgDown like in browsers
  • How to create OLE Automation to be used with ClearExplorer
  • How to get a time and Date Separately?
  • Signed Java web start application with Glassfish 4.1 and Java7
  • How to process remote XML files with XSLT
  • Query regarding com.jcraft.jsch.JSchException: UnknownHostKey: x.y.com. DSA key fingerprint is “ac:e
  • Fortran function variable length string return
  • Does hibernate load two seprate copies of same instance if they are loaded twice from database?
  • Autocomplete source from project settings
  • 'Edit' function for forum posts and such
  • How to use Flask's render_template from an ajax POST form submit
  • Generate and export point cloud from Project Tango
  • Silverlight Event Log in Isolated Storage
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Rotating Towards Path in OpenGL
  • Google Spreadsheet Script to Blink a range of Cells
  • C++ STL stack pop operation giving segmentation fault
  • r - input value by user to dataframe via shiny
  • JQuery: Infinite input select
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • How to turn off notice reporting in xampp?
  • How to specify generic type when the type is only known at runtime?
  • Android: Unable to detect vertical plane
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • Did not understand process of initialize in swift programming
  • How to encrypt Connectionstring written in web.config from codebehind?
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • Android Library Projects on Windows and Mac
  • How to use FirstOrDefault inside Include
  • WPF custom control and direct content support
  • PHP Permalinks.. how to change?
  • Running R's aov() mixed effects model from Python using rpy2
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ReferenceError: TextEncoder is not defined
  • Time Complexity of Fibonacci Algorithm [duplicate]