2017-04-05 70 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()是我的控制器功能。 –