我正在尝试在数据库中保存多个图像路径,并在root的upload / images [目录]中上传图像。我已经完成了它的工作。图像名称保存在数据库中,并且正在上载图像文件。我只是通过爆炸保存图像名称。现在我需要在视图中爆炸该图像。我怎样才能做到这一点?我在视图中尝试了以下代码,但它无法正常工作。
<?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>
它抛出了这个错误:
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>
在我的图片上传功能中有错误,这是我的代码:
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);
}
} 这是我获取数据的模型函数:
public function get_product()
{
#code
$query=$this->db->get('products');
return $query->result_array();
}
非常感谢任何形式的帮助。谢谢。
答案 0 :(得分:1)
你应该尝试这样的事情
<?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>
修改强>
我将在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>
答案 1 :(得分:0)
试试这个:
<?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>