显示使用codeigniter中的一对多关系存储的数据库中的图像

时间:2017-04-05 09:03:34

标签: php mysql codeigniter

我为单品上传了多张图片。这里的情况是我有products table和product_images表。这两个表是有关系的。 product_images表的product_id列是产品表的product_id列的外键。以下是详细代码。Code 现在我已经尝试过这种方式来访问图像。 在我的模型中,获取功能是:

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

我的观点是:

    div class="container">
    <h2>Product Lists</h2>

   <table class="table table-striped, table-hover">
   <thead>
    <tr>
      <td><strong>Product_Id</strong></td>
      <td><strong>Product_Name</strong></td>
      <td><strong>Picture</strong></td>
      <td><strong>Options</strong></td>
   </tr>
  </thead>
   <tbody>
   <?php foreach ($products as $p): ?>

  <tr>
      <td><?php echo $p['product_id']; ?></td>
      <td><?php echo $p['product_name']; ?></td>
      <!-- <td><?php echo $p['picture']; ?> </td> -->
      <td><img src="<?php echo base_url('uploads/images/').$images[0];?>" />                      </td>
         <td>
         <a href="<?php echo site_url('#'); ?>">View</a>
      </td>
      </tr>
        <?php endforeach; ?>
      </tbody>
     </table>

除了没有显示图像外,一切正常。它只是显示图像的裂缝缩略图。

这是我的存储功能:

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

1 个答案:

答案 0 :(得分:2)

型号:

  let jsonOb:JsonModel = self.ObjArray[Index]
  print("jsonOb = \(jsonOb.description())")

查看:

public function get_product()
{   
    $this->db->select("products.*, GROUP_CONCAT(images.images) as images", false);
    $this->db->from('products');
    $this->db->join('images', 'images.product_id=products.id', 'left');
    $this->db->group_by('products.id');
    $query=$this->db->get();
    return $query->result_array();
}