如何从代码点火器

时间:2017-04-06 04:33:21

标签: php codeigniter

我是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?
                         }
                     }
                 }
             }

这就是我的编辑页面现在的样子。Editpage 现在我想要发生的是,当我点击删除按钮然后该特定图片应该被删除,我应该能够上传附加图片与剩余的一个,我,e,这些都没有删除。 任何人都可以帮助我。任何形式的帮助都非常感谢。提前谢谢。

2 个答案:

答案 0 :(得分:1)

<强> LOGIC

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

  1. 点击删除链接
  2. 现在你有两个选择。  i)发送到URL并返回此页面  ii)使用Ajax

    1. 从图像表中删除图像
    2. 通过直接链接或通过Ajax,您将发送图像的ID,您可以通过在模型中创建功能轻松删除该ID。

      1. 从上传文件夹中删除图片
      2. 有一个内置的PHP函数unlink(path),用于删除文件。您可以使用相同的功能执行此操作,以便从images表中删除图像。

        如何编码

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

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

        但它只暗示你是否在product_images表中有图像。如果您在products表的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;
        

答案 1 :(得分:1)

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;
        }
    }
?>