Tạo App/Views như thế này
<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>
App/Controller
$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');Model chúng ta thiết lập như sau
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; } }
Lời kết
chúc các bạn làm thành công
0 comments:
Post a Comment