Welcome Guest, Not a member yet? Register   Sign In
Multiple delete image from database and folder too
#1

hey dear i just curious that about multiple delete,

here is my view using check box javascript

Code:
<form name="indonesia" action="<?php echo site_url('admin/wallpaper/delete'); ?>" method="post">  
        
         <button type="submit" class="btn btn-danger" name="hapus" value="hapus">Hapus</button>

          <?php echo anchor('admin/wallpaper/tambah', 'Tambah Wallpaper');?>
          
        <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                 <th>
                    <button type="button" class="btn btn-info" onClick="check_all()" >Check</button>
                    <button type="button" class="btn btn-success" onClick="uncheck_all()" >Un-Check</button>
                </th>
                <th>id</th>
                <th>Keterangan</th>
                <th>Gambar</th>
                <th>Action</th>
            </tr>
        </thead>
        
            <tbody>
                <?php
                foreach ($ListWallpaper->result() as $row)
                {
                ?>
            <tr>
                <td><input type="checkbox" name="item[]" id="item[]" value="<?=$row->id_wall ?>"></td>
                <td><?=$row->id_wall ?></td>
                <td><?=$row->ket ?></td>
                <td><?=$row->wall ?></td>
                <td>
                    <a href="<?php echo base_url() ?>admin/wallpaper/hapus/<?= $row->id_wall ?>" class="label label-success">Delete</a>
                    
                    <a href="<?php echo base_url() ?>admin/wallpaper/edit/<?= $row->id_wall ?>" class="label label-success">Update</a>
               </td>
            </tr>
           <?php  }  ?>
        </tbody>
    </table>
          
    </form>

and here is my controller

Code:
public function delete()
    {
        
    $ownerNames = $this->input->post('item');

    foreach ($ownerNames as $ownerName => $k) {
    
     //echo "Array : " . $k . "<br/>";
    
    $photo = $this->wallpaper_model->del_photo($k);
    
    if ($photo->num_rows() > 0)
        {
            $row = $photo->row();
            $file_photo = $row->wall;
            $path_file = 'image/wallpaper/';
            unlink($path_file.$file_photo);
        }
        
      $this->wallpaper_model->drop_photo($k);
      
      redirect('admin/wallpaper','refresh');
    }
}

model
Code:
function del_photo($k)
                {
                    $this->db->where('id_wall',$k);
                    
                    $query = $getData = $this->db->get('tabel_wall');
                            
                            if($getData->num_rows() > 0)
                                    
                                return $query;
                            
                            else
                                  
                               return null;
                }

                                                    
function drop_photo($k)
                                {
                                    $this->db->where('id_wall',$k);
                                    
                                    $this->db->delete('tabel_wall');
                                }


i dont get some error just delete one image in folder when i clicked all, what do i wrong in my MVC, but when i print_r or echo it $file_photo = $row->wall; <--show the name_file ?

anyone suggest for me to fix it, gonna big appreciate that and help others if they face like me


Thanks
Reply
#2

First of all, for your delete from the database, I wouldn't use a foreach statement. If you have all of the ids in an array as such:
PHP Code:
$ids = array(156910);
// I would do this:
$this->db->where_in('id'$ids);
$this->db->delete('table_name'); 

Also are you having a problem deleting multiple items from the database or multiple items from the images folder. If the problem is the database delete, the above code should help.
Reply
#3

However, since he's doing multiple actions (delete from db AND delete corresponding image), I WOULD do them in a loop. What if there is a file permission error or something when deleting one of the images, but you've already deleted all entries from the DB?
Reply
#4

(02-05-2015, 09:53 AM)CroNiX Wrote: However, since he's doing multiple actions (delete from db AND delete corresponding image), I WOULD do them in a loop. What if there is a file permission error or something when deleting one of the images, but you've already deleted all entries from the DB?

If he's deleting a couple of rows at a time a loop is no problem and easy. But since he can select them all and if he had a ridiculous number of rows he selected to delete, I'm not sure I would loop through and query the database like that. I would personally delete each image from the folder first and if the delete was successful, add it to an array of ids and then delete all of the rows at once for only images that were deleted from the folder successfully.
Reply
#5

(02-05-2015, 09:33 AM)egall8 Wrote: First of all, for your delete from the database, I wouldn't use a foreach statement. If you have all of the ids in an array as such:

PHP Code:
$ids = array(156910);
// I would do this:
$this->db->where_in('id'$ids);
$this->db->delete('table_name'); 

Also are you having a problem deleting multiple items from the database or multiple items from the images folder. If the problem is the database delete, the above code should help.

is there anyway to fix my problem, i just move native to codeigniter, if in nativa i always use foreach, would you make some tutorial about it, will big thanks to solve this so people who came with same problem will be easy to search in this forum, Thanks Big Grin
Reply
#6

(02-05-2015, 09:53 AM)CroNiX Wrote: However, since he's doing multiple actions (delete from db AND delete corresponding image), I WOULD do them in a loop. What if there is a file permission error or something when deleting one of the images, but you've already deleted all entries from the DB?


would you make some tutorial baout multiple delete from db and folder too, i new bie move came from native, will big thanks to know you if you will make tutorial and make people easy to searching out Big Grin
Reply
#7

(02-05-2015, 09:33 AM)egall8 Wrote: First of all, for your delete from the database, I wouldn't use a foreach statement. If you have all of the ids in an array as such:

PHP Code:
$ids = array(156910);
// I would do this:
$this->db->where_in('id'$ids);
$this->db->delete('table_name'); 

Also are you having a problem deleting multiple items from the database or multiple items from the images folder. If the problem is the database delete, the above code should help.

my problem how to delete images to from folder, when i clicked all it will success delete from db but in folder images still exist, how can i do delete from images too ?
Reply
#8

(This post was last modified: 02-06-2015, 02:00 AM by Avenirer.)

Working with what you've got so far we can do this:


The controller: 

PHP Code:
public function delete()
{
  $ownerNames $this->input->post('item');
 
 $messages = array();
 
 foreach($ownerNames as $owner)
 
 {
 
   $photo $this->wallpaper_model->get_photo($owner); // not del_photo because in the model you are only asking about a photo and not deleting it
 
   if ($photo !== false)
 
   {
 
     $path_file 'image/wallpaper/'.$photo->wall;
 
     if(unlink($path_file.$file_photo))
 
     {
 
       $messages[] = 'File '.$photo->wall.' deleted successfuly';
 
       if(!$this->wallpaper_model->drop_photo($owner))
 
       {
 
         $messages[] = 'The file '.$photo->wall.' couldn\'t be deleted from database';
 
       }
 
     }
 
     else
      
{
 
       $messages[] = 'Couldn\'t delete file '.$photo->wall;
 
     }
 
   }
 
   else
    
{
 
     $messages[] = 'Couldn\'t find image with id '.$owner;
 
   }
 
   redirect('admin/wallpaper','refresh');
 
 }



The model:

PHP Code:
function get_photo($id)
{
 
 $this->db->where('id_wall',$id);
 
 $this->db->limit(1);
 
 $query $this->db->get('tabel_wall');
 
 if($query->num_rows() > 0)
 
 {
 
   return $query->row();
 
 }
 
 else return false;
}
 
                                                   
function drop_photo($id)
{    
 
 if($this->db->delete('tabel_wall', array('id_wall' => $id))
 
 {
 
   return true;
 
 }
 
 return false;


As you can see, you can even announce the user that the files were deleted or not. You simply pass the $messages to a session flashdata, and at the redirected page you simply output the results.

Didn't verify if it works, but it should...

And this is how I would have done it:

In controller we have:


PHP Code:
$ownerNames $this->input->post('item');

$delete $this->wallpaper_model->del_photos($ownerNames);

if(
is_array($delete))
{
  
$this->session->set_flashdata('errors',$delete);
}
redirect('admin/wallpaper','refresh'); 

In model we then have:

PHP Code:
public function del_photos($ids)
{
  if(
is_array($ids) && sizeof($ids) > 0)
  {
    
$temp_ids = array();
    
$delete_ids = array();
    
$errors = array;
    
$this->db->where_in('id_wall',$ids);
    
$query $this->db->get('tabel_wall');
    if(
$query->num_rows()>0)
    {
      foreach(
$query->result() as $row)
      {
        
$temp_ids[$row->id_wall] = $row->wall;
      }
    }
    foreach(
$temp_ids as $id=>$file)
    {
      
$path_file 'image/wallpaper/'.$file;
      if(
unlink($path))
      {
        
$delete_ids[] = $id;
      }
      else
      {
        
$errors[] = 'Couldn\'t delete file '.$file;
      }
    }
    
$this->db->where_in('id_wall',$delete_ids);
    
$this->db->delete('tabel_wall');
    if(
sizeof($errors)>0)
    {
      return 
$errors;
    }
    return 
true;
  }


Of course this can be improved also...
Reply
#9

(02-06-2015, 12:34 AM)Avenirer Wrote: Working with what you've got so far we can do this:


The controller: 


PHP Code:
public function delete()
{
  $ownerNames $this->input->post('item');
 
 $messages = array();
 
 foreach($ownerNames as $owner)
 
 {
 
   $photo $this->wallpaper_model->get_photo($owner); // not del_photo because in the model you are only asking about a photo and not deleting it
 
   if ($photo !== false)
 
   {
 
     $path_file 'image/wallpaper/'.$photo->wall;
 
     if(unlink($path_file.$file_photo))
 
     {
 
       $messages[] = 'File '.$photo->wall.' deleted successfuly';
 
       if(!$this->wallpaper_model->drop_photo($owner))
 
       {
 
         $messages[] = 'The file '.$photo->wall.' couldn\'t be deleted from database';
 
       }
 
     }
 
     else
      
{
 
       $messages[] = 'Couldn\'t delete file '.$photo->wall;
 
     }
 
   }
 
   else
    
{
 
     $messages[] = 'Couldn\'t find image with id '.$owner;
 
   }
 
   redirect('admin/wallpaper','refresh');
 
 }



The model:


PHP Code:
function get_photo($id)
{
 
 $this->db->where('id_wall',$id);
 
 $this->db->limit(1);
 
 $query $this->db->get('tabel_wall');
 
 if($query->num_rows() > 0)
 
 {
 
   return $query->row();
 
 }
 
 else return false;
}
 
                                                   
function drop_photo($id)

 
 if($this->db->delete('tabel_wall', array('id_wall' => $id))
 
 {
 
   return true;
 
 }
 
 return false;


As you can see, you can even announce the user that the files were deleted or not. You simply pass the $messages to a session flashdata, and at the redirected page you simply output the results.

Didn't verify if it works, but it should...

And this is how I would have done it:

In controller we have:



PHP Code:
$ownerNames $this->input->post('item');

$delete $this->wallpaper_model->del_photos($ownerNames);

if(
is_array($delete))
{
 
 $this->session->set_flashdata('errors',$delete);
}
redirect('admin/wallpaper','refresh'); 

In model we then have:


PHP Code:
public function del_photos($ids)
{
 
 if(is_array($ids) && sizeof($ids) > 0)
 
 {
 
   $temp_ids = array();
 
   $delete_ids = array();
 
   $errors = array;
 
   $this->db->where_in('id_wall',$ids);
 
   $query $this->db->get('tabel_wall');
 
   if($query->num_rows()>0)
 
   {
 
     foreach($query->result() as $row)
 
     {
 
       $temp_ids[$row->id_wall] = $row->wall;
 
     }
 
   }
 
   foreach($temp_ids as $id=>$file)
 
   {
 
     $path_file 'image/wallpaper/'.$file;
 
     if(unlink($path))
 
     {
 
       $delete_ids[] = $id;
 
     }
 
     else
      
{
 
       $errors[] = 'Couldn\'t delete file '.$file;
 
     }
 
   }
 
   $this->db->where_in('id_wall',$delete_ids);
 
   $this->db->delete('tabel_wall');
 
   if(sizeof($errors)>0)
 
   {
 
     return $errors;
 
   }
 
   return true;
 
 }


Of course this can be improved also...



Thanks a lot guys, you rock, why this forum don't have notification when some user answer it,
Reply




Theme © iAndrew 2016 - Forum software by © MyBB