如何在数据库中检索内爆图像路径以在代码点火器中查看 [英] How to retrieve imploded images path in database to view in code igniter
问题描述
我正在尝试在数据库中保存多张图片路径,并将图片上传到root的upload / images [目录]中。我已经完成了它的工作。图像名称已保存在数据库中,并且正在上传图像文件。我只是通过内爆来保存图像名称。现在,我需要分解该图像。我怎样才能做到这一点?我已经尝试了以下代码,但无法正常工作。
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
是我的第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.
推荐答案
您应该尝试类似的事情
<?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>
编辑
我将通过此链接
<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>
这篇关于如何在数据库中检索内爆图像路径以在代码点火器中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!