使用CodeIgniter更新和删除图像

时间:2015-10-13 11:30:55

标签: php html 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 -->

如何在代码上实现功能更新和删除图像?

1 个答案:

答案 0 :(得分:0)

在控制器中

public function delete($id) {

    if(empty($id)){
        redirect('city');
    }
    else
    {

        $this->load->model('m_city');
        $res = $this->m_city->delete_data($id);
        if($res == 0){
            redirect('city');
        }
        else
        {
            $res['data'] = $this->m_city->select_data($id);

            if(empty($res['data']))
            {
                redirect('city');
            }
            else
            {
                $this->load->view('design/header');
                $this->load->view('city/delete',$res);
                $this->load->view('design/footer');         
            } 
        }  
    }  
}

public function update_view($id) { 

    if($this->validate() == TRUE)
    {
        $this->load->model('m_city');
        $res = $this->m_city->select_data($id);
        if(empty($res))
        {
            redirect('city');
        }
        else
        {
            #load the View of Edit
        }
    }
    else
    {
        redirect('city/edit/' . $id);
    }
}

public function update($id) { 

    if($this->validate() == TRUE)
    {
        $this->load->model('m_city');
        $res = $this->m_city->update_data();
        if($res == 0)
        {
            redirect('city');
        }
        else
        {
            echo 'Edit Submitted';
        }
    }
    else
    {
        redirect('city/edit/' . $id);
    }
}

在模型中

function delete_data($id) {

    if(!$this->db->delete('mytable', array('id' => $id))) //provide table name
    {
        return $log = 0;
    }
    else
    {
        return $log = ;
    }       
}

function select_data($id) {

    if(!empty($id))
    {
        $this->db->where('id', $id);
        $query = $this->db->get('m_city');
    }   
    else
    {
        $query = $this->db->get('m_city');
    }     

    return $query->result_array();   
}

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

    if($this->db->update('m_city', $data))
    {
        return $log = 1;
    }
    else
    {
        return $log = 0;
    }

}