2017-04-05 112 views
0

我是新代码点火器。 这是我想要做的。我有存储在数据库表名产品中的产品列表。对于我需要插入多个图像的每个产品。我创建了两个表格,产品和productimage。我已经将表productimage的product_id作为外键,引用表产品的product_id。现在我想从表单中保存数据。这是我以前做过的Saving images in a single row by imploding 但是对我来说,管理CRUD变得相当困难(就像编辑和删除图片一样)。 所以我想要做上述提到的方式。我没有找到开始的方法。任何人都可以请教我,我该如何开始?在codeigniter中使用一对多保存多个图像

好吧,现在我已经在这里做了一些编码。这是我的控制器:

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 

    class Products extends CI_Controller{ 

    public function __construct() 
    { 
    parent::__construct(); 
    $this->load->model('product_model'); 
    $this->load->helper(array('form','url')); 
    //Codeigniter : Write Less Do More 
    } 

    public function index() 
    { 
    $data['products']=$this->product_model->get_product(); 
    $this->load->view('/landing_page',$data); 
    } 

    public function create() 
    { 
    #code 
    $this->load->helper('form'); 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('product_name','Product_Name','required'); 

    if($this->form_validation->run()=== FALSE) 
    { 
    $this->load->view('products/create'); 
    } 
    else { 
    $this->product_model->set_product(); 
    $data['products']=$this->product_model->get_product(); 
    redirect('/'); 
    } 
} 
} 

这是我的模型:当我的表单打开我被能填补产品名称,上传图片文件

  <?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 

    class Product_model extends CI_Model{ 

    public function __construct() 
    { 
    $this->load->database(); 
    parent::__construct(); 
    //Codeigniter : Write Less Do More 
    } 

    public function get_product() 
    { 
    #code 
    $query=$this->db->get('products'); 
    return $query->result_array(); 
    } 

    public function set_product($id=0) 
    { 
    #code 
    // if($this->input->post('userSubmit')){ 
     $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']; 
      } 
      else{ 
       $picture = ''; 
      } 
     } 
     } 
     }//end of first if 

     else{ 
      $picture = ''; 
     } 

    $data=array(
    'product_name'=>$this->input->post('product_name') 
); 
    $picture=array(
    'product_id'=>$this->db->get('products',$id), 
    'picture_image'=>$picture 
); 
    if ($id==0) 
    { 
    return $this->db->insert('products',$data); 
    return $this->db->insert('images',$picture); 
    } 
    else { 
    $this->db->where('id',$id); 
    return $this->db->update('products',$data); 
    return $this->db->update('images',$picture); 
    } 
    } 
    } 

诺埃的情况。当我提交它时也不会引发任何错误。但是只有产品名称存储在产品表中,并且图像表没有任何反应。没有插入任何图像。浏览器不会引发任何错误。简单图像 表是空的。这里有什么问题?

+0

首先,您可以用最少的必填字段创建产品,然后将图像上传到该产品。在magento中,他们是这样做的。 – kishor10d

+0

哦,你不上传模型中的图像。虽然CI不会阻止你。建议您在控制器上传图片 –

回答

0

因为任何人都有同样的问题,那么这里是解决方案。 (我的朋友Amani Ben azzouz的代码)

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

这也是为了插入和更新。如果你只是想插入只是删除参数作为'ID'传递,并削减,如果和其他部分写一个明确的代码里面'如果'。

1

让我帮你控制器..你需要检查所有上传的文件。他们是$ _FILES。循环访问阵列,将它们上传到服务器上,然后调用模型函数将它们添加到产品中。图像表

如果CI上传对您来说太棘手。使用以下控制器功能

public function upload_images() 
{ 
// following IF statement only checks if the user is logged in or not 
if($this->session->userdata['id'] && $this->session->userdata['type']=='user') 
{ 
    if($_FILES) 
    { 
     // check whether there are files uploaded/posted 
     if(isset($_FILES['files'])){ 
      $data['errors']= array(); 
      $extensions = array("jpeg","jpg","png"); 

      //Loop through the uploaded files 

      foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){ 

       $file_name = $key.$_FILES['files']['name'][$key]; 
       $file_size =$_FILES['files']['size'][$key]; 
       $file_tmp =$_FILES['files']['tmp_name'][$key]; 
       $i=1; 
       if($file_size > 2097152){ 
        $data['errors'][$i]='File '.$i.' size must be less than 2 MB'; 
        $i++; 
       } 
       // Set upload destination directory 
       $desired_dir="uploads"; 
       if(empty($data['errors'])==true){ 
        if(is_dir($desired_dir)==false){ 
         mkdir("$desired_dir", 0700);  // Create directory if it does not exist 
        } 
        if(is_dir("$desired_dir/".$file_name)==false){ 
         // Upload the file. 
         move_uploaded_file($file_tmp,"uploads/".$file_name); 
         // Call a function from model to save the name of the image in images table along with entity id 
         $this->post_model->addImage('property_images',$file_name,$this->uri->segment(3)); 
        }else{         //rename the file if another one exist 
         $new_dir="uploads/".$file_name.time(); 
         rename($file_tmp,$new_dir) ; 
        } 
       }else{ 
        $data['contact']=$this->admin_model->getContactDetails(); 
        $data['images']=$this->post_model->getPropertyImages($this->uri->segment(3)); 
        //load views 
       } 
      } 
      if(empty($data['errors'])) 
      { 
       redirect(base_url().'dashboard'); 
      } 
      else 
      { 
       $data['contact']=$this->admin_model->getContactDetails(); 
       $data['images']=$this->post_model->getPropertyImages($this->uri->segment(3)); 
       //load views 
      } 
     } 
    } 
    else 
    { 
     //Load view 
    } 
} 
else 
{ 
    redirect(base_url().'user/login'); 
} 

} 
+0

谢谢先生,但我有一个问题。首先让我编辑我的问题,我已经完成了。然后指导我。 –

+0

你看到我的代码了吗?如果可以,请提供一些帮助。 –

+0

请看看我的代码。 upload_images()是我的控制器功能。 –