如何检索数据库中的内爆图像路径以在 codeigniter 中查看
How to retrieve imploded images path in database to view in code igniter
我试图在数据库中保存多张图片路径,并在 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();
}
非常感谢任何形式的帮助。谢谢。
你应该试试这样的东西
<?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>
试试这个:
<?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>
我试图在数据库中保存多张图片路径,并在 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();
}
非常感谢任何形式的帮助。谢谢。
你应该试试这样的东西
<?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>
试试这个:
<?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>