Welcome Guest, Not a member yet? Register   Sign In
Multiple delete image from database and folder too
#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


Messages In This Thread
RE: Multiple delete image from database and folder too - by Avenirer - 02-06-2015, 12:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB