2015-02-05 1 views
1

데이터베이스에서 이미지를 삭제하고 이미지 저장소 인 폴더에서도 코드를 만들었지 만 "모두 체크"를 클릭하면 데이터베이스와 폴더에서 하나의 이미지 만 성공적으로 삭제됩니다. . 나는 무엇을 잘못 했는가? 여기 내보기가 사용 확인란 자바 스크립트 : 여기codeigniter를 사용하여 데이터베이스에서 여러 이미지 삭제

<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> 

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'); 
      } 

만 하나 개의 이미지가 성공적으로 너무 폴더와 데이터베이스에서 삭제됩니다 내 컨트롤러

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; 

     echo "$file_name</br>"; 
     $path_file = 'image/wallpaper/'; 

     unlink($path_file.$file_photo); 
    } 

    $this->wallpaper_model->drop_photo($k); 

    redirect('admin/wallpaper','refresh'); 
} 
} 

내 모델입니다 , 그러나하려고하면 echo "$file_name</br>"; 모든 이미지를 표시합니다. 나는 무엇을 잘못 했는가? 누구든지 나를 인도 할 수 있다면, 나는 그것을 감사 할 것이다.

답변

1

우선 나는 SITE_URL의 일부, $data['wallpapers'] = array();을 볼 컨트롤러에서 이미지의 배열을 설정합니다 array() 및 값 확인 상자에서 이름 = ""

면책 조항 : 이것은 예제 일뿐입니다.

public function index() { 
    $k = $this->uri->segment(what ever); // Use uri segment is id example.com/image/1 = $this->uri->segment(2); 

    $results = $this->model_name->del_photo($k); 

    $data['wallpapers'] = array(); 

    foreach ($results as $result) { 
     $data['wallpapers'][] = array(
     'id_wall' => $result['id_wall'], 
     'update' => site_url('controllername/update' .'/'. $result['wall_id']), 
     'ket' => $result['ket'] 
    ); 
    } 

    $data['delete'] = site_url('controller/function'); 

    $selected = $this->input->post('selected'); 

    if (isset($selected)) { 
     $data['selected'] = (array)$selected; 
    } else { 
     $data['selected'] = array(); 
    } 

    $this->load->view('your list', $data); 
} 
public function update() { 
    //update info here 
} 

public function delete() { 

    $selected = $this->input->post('selected'); 

    if (isset($selected)) { 
     foreach ($selected as $image_id) { 

      $wall_id = $image_id 

      $this->db->query("DELETE FROM " . $this->db->dbprfix . "TABLENAME WHERE wall_id = '" . (int)$wall_id . "'"); 

     } 
    } 

} 

보기

추가 된 사이트의 URL과 컨트롤러에서 삭제 에코.

<form action="<?php echo $delete;?>" method="post"> 

<table> 

<thead> 
<tr> 

<td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td> 

<td>Update</td>  
</tr> 

</thead> 
<tbody> 



<?php if ($wallpapers) { ?> 

<?php foreach ($wallpapers as $wallpaper) { ?> 

<td class="text-center"><?php if (in_array($wallpaper['id_wall'], $selected)) { ?> 

<input type="checkbox" name="selected[]" value="<?php echo $wallpaper['id_wall']; ?>" checked="checked" /> 

<?php } else { ?> 

<input type="checkbox" name="selected[]" value="<?php echo $wallpaper['id_wall']; ?>" /> 

<?php } ?> 

</td> 
<td><a href="<?php echo $update; ?>">Update</a></td> 

<?php } ?> 

<?php } ?> 



</tbody> 

</table> 

</form> 

모델

function del_photo($k) { 

$this->db->where('id_wall',$k); 

$query = $this->db->get('table_name'); 

if($query->num_rows() > 0) { 

    $return = $query->result_array(); 

} else { 

    return false; 
} 

} 
+0

당신이 전경을 쓸 수 있습니다 어디 형태의 작업은 그래서 난 당신에게 더 나은 아이디어를주고 내가 컨트롤러의 배열의 업데이트 기능을 추가 mustang83 –

+0

@ 컨트롤러와 모델을 이해할 수 있고 보기에서 – user4419336

+0

또한 삭제 기능에서 쿼리를 추가했습니다 – user4419336

0

컨트롤러에서 foreach 루프 외부로 redirect 지시문을 이동합니다. $route['controller/update/(:any)'] = "controller/update/$1"

선택한 이미지를 삭제하려면 선택한 게시물을 할 수있는 당신이 당신의 route.php에 포함해야 할 수도 있습니다

관련 문제