0
有些日子我一直在尋找教程來解決這個問題,但我還沒有找到。使用CodeIgniter更新和刪除圖片
控制器:City.php
public function edit() {
$id = $this->uri->segment(3);
if(empty($id)){
redirect('city/add');
}
$this->load->model('m_city');
$res['data'] = $this->m_city->select_data($id);
if(empty($res['data'])){
redirect('city/add');
}
$this->load->view('design/header');
$this->load->view('city/edit',$res);
$this->load->view('design/footer');
}
public function update() {
$id = $this->uri->segment(3);
if($this->validate() == TRUE){
$this->load->model('m_city');
$res = $this->m_city->update_data();
if($res){
redirect('city');
}
}else{
redirect('city/edit/' . $id);
}
}
public function delete() {
$id = $this->uri->segment(3);
if(empty($id)){
redirect('city');
}
if($this->input->post('id')){
$this->load->model('m_city');
$res = $this->m_city->delete_data();
if($res){
redirect('city');
}
}
$this->load->model('m_city');
$res['data'] = $this->m_city->select_data($id);
if(empty($res['data'])){
redirect('city');
}
$this->load->view('design/header');
$this->load->view('city/delete',$res);
$this->load->view('design/footer');
}
型號:M_city.php
function select_data($id = NULL) {
if(!empty($id)){
$this->db->where('id', $id);
}
$query = $this->db->get('m_city');
return $query->result();
}
function update_data() {
$data = array(
'name' => $this->input->post('name'),
'description' => $this->input->post('description'),
'name_image' => $name_image
);
$this->db->where('id', $this->input->post('id'));
$this->db->update('m_city', $data);
return true;
}
function delete_data() {
$this->db->delete('m_city', array('id' => $this->input->post('id')));
return true;
}
查看:edit.php
<section class="content">
<div class="row">
<div class="col-lg-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Edit City</h3>
</div><!-- /.box-header -->
<!-- form start -->
<?php
$id = $this->uri->segment(3);
$attributes = array('role' => 'form', 'id' => 'formedit');
echo form_open('city/update/'.$id,$attributes);
?>
<div class="box-body">
<?php
echo validation_errors('<div class="alert alert-danger"><button class="close" data-dismiss="alert" type="button">×</button>','</div>');
foreach ($data as $val) {
$name = $val->name;
$description = $val->description;
}
?>
<input type="hidden" name="id" value="<?php echo $id;?>" />
<div class="form-group">
<label for="nm_city">City</label>
<input type="text" class="form-control" value="<?php echo $name; ?>" id="name" name="name" placeholder="City">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" value="<?php echo $description; ?>" id="description" name="decription" placeholder="Description">
</div>
<div class="form-group">
<label for="deal_descriptions">Image City</label>
<img width="200" src="<?= base_url(); ?>uploadfiles/<?php echo $name_image; ?>">
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Simpan</button>
<a href="<?=base_url() . 'city';?>" class="btn btn-default">Batal</a>
</div>
<?php echo form_close(); ?>
</div><!-- /.box -->
</div>
</div> <!-- /.row -->
</section><!-- /.content -->
如何實現功能更新和刪除的圖像碼?我非常感謝你的幫助。
第一'的unlink()'的文件,然後從數據庫中刪除。 –
我應該把unlink()放在哪裏?那麼寫作如何?謝謝 –
@PiedrosaImoes你能否提供給我查看?? –