I’m trying to set up an admin page where I’ll be able to select images from a gallery with check boxes in a web-form then remove the images from the file system as well as the database. The issue I’m having is how I should update the database.
As you can see in the model, I currently update the database after each file was removed, the just keep count of how many were removed and subtracting that from the “old” order number since image 23 after 10 images before it were removed would end up being image 23-10 = 13). Not sure if there might be a few break cases where this wont work.
It seems to work but noticed if the gallery has a lot of pictures the database order id doesn’t get updated after 35. I’m wondering if that might be some limit like how many connections can be made to the database in a given amount of time? Is there a way I could store all the order id’s and update them all at once or is making a connection to the database each time correct(I’m doubting since that seems like bad practice)?
The web-form:
<form action="http://localhost/admin/galleryRemove" method="post" enctype="multipart/form-data">
<input type="checkbox" name="orderID[]" value="1" />
<img src="http://localhost/img/galleries/001/img01.jpg" alt="1" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="2" />
<img src="http://localhost/img/galleries/001/img02.jpg" alt="2" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="3" />
<img src="http://localhost/img/galleries/001/img03.jpg" alt="3" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="4" />
<img src="http://localhost/img/galleries/001/img04.jpg" alt="4" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="5" />
<img src="http://localhost/img/galleries/001/img05.jpg" alt="5" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="6" />
<img src="http://localhost/img/galleries/001/img06.jpg" alt="6" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="7" />
<img src="http://localhost/img/galleries/001/img07.jpg" alt="7" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="8" />
<img src="http://localhost/img/galleries/001/img08.jpg" alt="8" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="9" />
<img src="http://localhost/img/galleries/001/img09.jpg" alt="9" class="imgCheck" />
<input type="checkbox" name="orderID[]" value="10" />
<img src="http://localhost/img/galleries/001/img10.jpg" alt="10" class="imgCheck" />
<input type="hidden" name="galleryID" value="1" />
<input type="submit" name="remove" value="Remove" /> This will remove all images checked.
</form>
CI model for processing the form post.
// Start of removing an image functions
function imgRemove()
{
$cnt = 0;
$id = $this->input->post('galleryID');
foreach ($_POST['orderID'] as $order)
{
$order = $order - $cnt;
// get the picture name
$this->db->select('*');
$this->db->where("gallery_id = '$id'");
$this->db->where("`order` = '$order'");
$this->db->from('gallery');
$q = $this->db->get();
if ($q->num_rows != 0)
{
$result = $q->result();
$picture = $result[0]->picture;
// Get the path and then unlink(delete) the file and it's thumbnail
$imgPath = $this->imgPath($id);
$img = $imgPath . $picture;
$thumbImg = $imgPath . 'thumbs/thumb' . $picture;
if (file_exists($img))
{
unlink($img);
}
if (file_exists($thumbImg))
{
unlink($thumbImg);
}
// Remove the line from the database
$this->db->where('gallery_id', $id);
$this->db->where('order', $order);
$this->db->delete('gallery');
// Reorder all images after the deleted one.
$this->imgOrderCheckRemove($id, $order);
$cnt++;
}
}
}
Database table layout:

Results from var_dump($images); right before the update_batch.
array(3) {
[2]=>
array(5) {
["prim_id"]=>
string(1) "8"
["gallery_id"]=>
string(1) "2"
["picture"]=>
string(21) "002.jpg"
["order"]=>
string(1) "2"
["alt_text"]=>
string(1) "2"
}
[10]=>
array(5) {
["prim_id"]=>
string(2) "16"
["gallery_id"]=>
string(1) "2"
["picture"]=>
string(18) "010.jpg"
["order"]=>
string(2) "10"
["alt_text"]=>
string(2) "10"
}
[13]=>
array(5) {
["prim_id"]=>
string(2) "19"
["gallery_id"]=>
string(1) "2"
["picture"]=>
string(15) "013.jpg"
["order"]=>
string(2) "13"
["alt_text"]=>
string(2) "13"
}
}
Heres something. I might have missed something as i wrote this pretty much on the fly, but it should work quite okay.