【问题标题】:how to delete(actually edit) images from edit page in code igniter如何从codeigniter中的编辑页面删除(实际编辑)图像
【发布时间】:2017-04-06 13:24:39
【问题描述】:

我是codeigniter的新手,所以我对它的了解很少。我需要从编辑页面编辑帖子。除了我上传的图片,我可以编辑所有内容。对于图片,我要么必须重新上传我想要的所有图片,要么图像字段将变为空。这是我当前的编辑代码:

    <body>

        <div class="container">
                                <strong>Edit Product</strong>
                      <?php echo validation_errors(); ?>
                                <?php echo form_open_multipart('/products/edit/'.$products['product_id']); ?>

                        <?php $error = form_error("product_name", "<p class='text-danger'>", '</p>'); ?>

                                    <div class="form-group <?php echo $error ? 'has-error' : '' ?>">
                                        <label for="product_name">Product_Name</label>
                                            <input type="text" name="product_name" value="<?php echo $products['product_name'] ?>" id="product_name" class="form-control">
                                        </div>



                              <div class="form-group">
                                  <label>Picture</label>
                                  <input class="form-control" type="file" name="picture[]" multiple />
                              </div>

                              <div class="container">
                            <?php

                                // explode images into a variable
                                $images=explode(',',$show['images']);
                             ?>
                             <?php foreach($images as $key=>$val){ ?>
                              <?php //echo ($val);?>
                               <div class="row">
                                <div class="col-sm-6">
                              <img src="<?php echo base_url('uploads/images/').$val;?>" />
                              <input class="btn btn-danger" type="submit" value="Remove" />
                                </div>
                               </div>
                               <?php } ?>
                            </div>



                                    <input type="submit" value="Create Product" class="btn btn-primary">
                                <?php echo form_close(); ?>

            </div>

      </body>

这是我在控制器上的编辑功能:

                  public function edit($id)
          {
              #code
              $data['show']=$this->product_model->get_product_by_id($id);
              if(empty($data['show']))
              {
                show_404();
              }
              $this->load->helper('form');
              $this->load->library('form_validation');
              $data['products']=$this->product_model->get_product_by_id($id);
              $this->form_validation->set_rules('product_name','Product_Name','required');
              $this->form_validation->set_rules('product_price','Product_Price','required');
              $this->form_validation->set_rules('produce_description','Produce_Description','required');

              if($this->form_validation->run() === FALSE)
              {
                $this->load->view('products/edit',$data);
              }

              else {
                $this->product_model->set_product($id);
                redirect('/');
              }
          }

这是我的 set_product 函数,用于将所有内容保存在数据库中:

    public function set_product($id=0){
                 $picture=array();
                 $count=count($_FILES['picture']['name']);
                 //Check whether user upload picture
                 if(!empty($_FILES['picture']['name'])){
                     foreach($_FILES as $value){
                         for($s=0; $s<=$count-1; $s++){
                             $_FILES['picture']['name']=$value['name'][$s];
                             $_FILES['picture']['type']    = $value['type'][$s];
                             $_FILES['picture']['tmp_name'] = $value['tmp_name'][$s];
                             $_FILES['picture']['error']       = $value['error'][$s];
                             $_FILES['picture']['size']    = $value['size'][$s];
                             $config['upload_path'] = 'uploads/images/';
                             $config['allowed_types'] = 'jpg|jpeg|png|gif';
                             $config['file_name'] = $_FILES['picture']['name'];

                             //Load upload library and initialize configuration
                             $this->load->library('upload',$config);
                             $this->upload->initialize($config);
                             // print_r($value['name'][$s]);exit;
                             if($this->upload->do_upload('picture')){
                                 $uploadData = $this->upload->data();
                                 $picture[] = $uploadData['file_name'];
                             }
                         }
                     }
                 }//end of first if
                 $data=array('product_name'=>$this->input->post('product_name'));

                 if ($id==0){
                      $this->db->insert('products',$data);
                      $last_id = $this->db->insert_id();
                      if(!empty($picture)){
                          foreach($picture as $p_index=>$p_value) {
                             $this->db->insert('images', array('product_id'=>$last_id,'images'=>$p_value));
                          }
                      }
                 }
                 else {
                     $this->db->where('id',$id);
                     $this->db->update('products',$data);
                     if(!empty($picture)){
                         foreach($picture as $p_index=>$p_value) {
                            $this->db->update('images', array('product_id'=>$last_id,'images'=>$p_value) ); // --> this one?
                         }
                     }
                 }
             }

这就是我的编辑页面现在的样子。 现在我想在这里发生的是,当我单击删除按钮时,只应删除该特定图片,并且我应该能够上传其他图片以及剩余的图片,即未删除的图片。 谁能帮助我。任何形式的帮助都将受到高度赞赏。提前致谢。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

逻辑

首先,了解在这种情况下实际应该发生什么的逻辑/概念。

  1. 点击删除链接

现在您有两个选择。 i) 发送到 URL 并返回此页面 ii) 使用 Ajax

  1. 从图像表中删除图像

通过直接链接或通过 Ajax,您将发送图像 ID,您可以通过在模型中创建函数轻松删除该 ID。

  1. 从上传文件夹中删除图片

有一个内置的 PHP 函数 unlink(path) 用于删除文件。您可以在您将编写的用于从图像表中删除图像的相同函数中执行此操作。

如何编码

在显示图像的视图中,您可以链接到类似的 URL

<a href="<?php echo base_url().'products/delImage/'.$images['id']?>" class="btn btn-danger">Remove</a>

但这仅暗示您在 product_images 表中是否有图像。如果您的产品表中的图像列中有 csv 格式的图像名称。您需要将 product_id 和图像名称发送到模型函数,然后使用str_replace() 函数从列中删除图像名称。

删除图片后,您可以使用重定向回到此页面

redirect($_SERVER['HTTP_REFERER']);

编辑

如果您有图像表。您可以划分您的 getProducts 查询,例如 (我的表是属性)

$data=$this->db->query('SELECT property.*, users.name as posted_by, property_types.name as property_type
     from property
     inner join users on users.id=property.posted_by
     inner join property_types on property_types.id=property.type
     order by property.posted_date desc
     ')->result_array();

 for($i=0;$i<count($data);$i++)
 {
      $data[$i]['images']=$this->db->query('SELECT * from property_images WHERE property_images.property_id='.$data[$i]['id'])->result_array();
 }

 return $data;

【讨论】:

  • 谢谢先生,我会试试这个。而且你的链接没有打开。是的,我需要学习很多东西。我才刚刚开始。 :)
  • $this->db->select("products.*, GROUP_CONCAT(images.images) as images", false); $this->db->from('products'); $this->db->join('images', 'images.product_id=products.product_id', 'left'); if($id) { $this->db->where('products.product_id', $id); } $this->db->group_by('products.product_id'); $query=$this->db->get();返回 $query->row_array();这是我用于选择的查询,我知道它不返回 image_id。我应该使用什么 SQL 函数来选择多个带有连接的列。
  • 最好的方法是将查询分成两部分。首先获取产品,遍历它并通过产品 id 获取图像并将其存储在索引中
  • 你能用它的 SQL 查询打我吗?我收到了您所说的内容,但无法查询。
  • 谢谢先生。 :) 你让我摆脱了一个巨大的问题。
【解决方案2】:
This is the controller function to delete image and its entry from the database:
<?php
public function delete()
{
   $id = $_GET['id'];
   $imageName = $_GET['img_name'];
   $imgDir = "uploads/";
   $delImage = $imgDir.$imageName;
   unlink($delImage);
   $result = $this->admin_model->deleteImage($id);
   if($result)
   {
     $this->session->set_flashdata('success_msg','<strong>This Image is deleted successfully.</strong>');
   }   
}
?>

This is the model function to delete image and called in the controller:
<?php
   public function deleteImage($id)
   {
        if($id!='') 
        {
          $data = array('id' => $id );
          $suc  = $this->db->delete('theme_master', $data);
        }

        if($suc){               
           return true;
        }else{
           return false;
        }
    }
?>

【讨论】:

  • $id = $_GET['id']; $imageName = $_GET['img_name'];这两行代码不起作用。这段代码应该如何获取 id 和图像名称?我是说从哪里来的?
  • 浏览器抛出此错误:未定义索引:img_name
  • 你需要像这样通过查询字符串传递值:移除
  • 如何获取图片名称? $_GET['image_name'];这里的 image_name 是什么?
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
相关资源
最近更新 更多