How to retrieve imploded images path in database t

2020-05-08 06:51发布

I am trying to save the multiple images path in database and upload images in root's upload/images[directory]. I have done it and its working. Images name are saved in database and and the image files are being uploaded. I just did save the images name by imploding. Now I need to explode that image in view. How can i do that? I have tried the following code in view and its not working.

      <?php foreach ($products as $p): ?>
      <tr>
          <td><?php echo $p['id']; ?></td>
          <td><?php echo $p['product_name']; ?></td>
          <td><?php echo $p['product_price']; ?></td>
          <td><?php echo $p['produce_description']; ?></td>
          <!-- <td><?php echo $p['picture']; ?> </td> -->
          <td><img src="<?php echo base_url('uploads/images/').explode('|',$p['picture']);?>" /> </td>
          <td>
              <a href="<?php echo site_url('products/view/'.$p['id']); ?>">View</a> |
              <a href="<?php echo site_url('products/edit/'.$p['id']); ?>">Edit</a> |
              <a href="<?php echo site_url('products/delete/'.$p['id']); ?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a>
          </td>
      </tr>

It throws this error:

    A PHP Error was encountered

    Severity: Notice

    Message: Array to string conversion

    Filename: views/dashboard.php

     Line Number: 47

and this is my line 47:

     <td><img src="<?php echo base_url('uploads/images/').explode('|',$p['picture']);?>" /> </td>

Incase there is mistake in my image upload function, here is my code for it:

    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'),
    'produce_description'=>$this->input->post('produce_description'),
    'product_price'=>$this->input->post('product_price'),
    'picture'=>implode('|',$picture)
  );
  if ($id==0)
  {
    return $this->db->insert('products',$data);
  }
  else {
    $this->db->where('id',$id);
    return $this->db->update('products',$data);
  }

} And this is my model function for getting data:

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

Any kind of help are highly appreciated. Thank you.

2条回答
霸刀☆藐视天下
2楼-- · 2020-05-08 07:15

You should try something like this

<?php foreach ($products as $p): ?>
  <?php 
      // explode images into a variable/array
      $images=explode('|',$p['picture']);
   ?>
  <tr>
      <td><?php echo $p['id']; ?></td>
      <td><?php echo $p['product_name']; ?></td>
      <td><?php echo $p['product_price']; ?></td>
      <td><?php echo $p['produce_description']; ?></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('products/view/'.$p['id']); ?>">View</a> |
          <a href="<?php echo site_url('products/edit/'.$p['id']); ?>">Edit</a> |
          <a href="<?php echo site_url('products/delete/'.$p['id']); ?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a>
      </td>
  </tr>

Edit

I will give you an example from a code in production at this link

<div class="aa-properties-details-img" style="margin-bottom: 25px;">
<?php
    $property[0]['images']=explode(',',$property[0]['img']);
    if(count($property[0]['images'])>0){
       for($i=0;$i<count($property[0]['images']);$i++)
       { ?>
        <img src="<?php echo  base_url().'img/'.$property[0]['images'][$i]?>" alt="img">
       <?php }
       }else{
       ?>
        <img src="<?php echo base_url().'img/no-image.jpg'?>" alt="img">
       <?php
     }
?>
</div>
查看更多
我命由我不由天
3楼-- · 2020-05-08 07:26

Try this:

<?php foreach ($products as $p): ?>
  <tr>
      <td><?php echo $p['id']; ?></td>
      <td><?php echo $p['product_name']; ?></td>
      <td><?php echo $p['product_price']; ?></td>
      <td><?php echo $p['produce_description']; ?></td>
      <td>
       <?php  $images=explode('|',$p['picture']); 
          foreach($images as $image) {
       ?>
      <img src="<?php echo base_url('uploads/images/').$image; ?>" width="50" height="50" />
      <?php } ?>
      </td>
      <td>
          <a href="<?php echo site_url('products/view/'.$p['id']); ?>">View</a> |
          <a href="<?php echo site_url('products/edit/'.$p['id']); ?>">Edit</a> |
          <a href="<?php echo site_url('products/delete/'.$p['id']); ?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a>
      </td>
  </tr>
查看更多
登录 后发表回答