Php 如何从数据库中分解逗号分隔的数组
我无法在codeigniter框架中从数据库中分解逗号分隔的数据数组。我想用字符串值将数组回送到多行中,如:Php 如何从数据库中分解逗号分隔的数组,php,codeigniter,Php,Codeigniter,我无法在codeigniter框架中从数据库中分解逗号分隔的数据数组。我想用字符串值将数组回送到多行中,如: --------------- Product Name --------------- Product 1 Product 2 Product 3 但我得到了数组到字符串的转换错误 <?php foreach ($res as $key => $value) { ?> <tr class="border-bottom">
---------------
Product Name
---------------
Product 1
Product 2
Product 3
但我得到了数组到字符串的转换错误
<?php foreach ($res as $key => $value) { ?>
<tr class="border-bottom">
<td>
<?php $prodArray = $value->product;
echo explode(',',$prodArray) ?>
</td>
</tr>
<?php } ?>
那么,如何分解和获取codeigniter视图文件中的数据呢
<table>
<th>
<td>S. NO</td>
<td>Product Name</td>
</th>
<tbody>
<?php
$no = 1;
foreach($products as $product){
?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $product->product_name;?></td> //update with your column name
</tr>
<?php
$no++;}
?>
</tbody>
</table>
控制器文件
public function product()
{
$this->load->model('product'); // can be loaded in the parent::__construct(); at the begining of the controller
$this->products = $this->product->get_products();
$this->load->view('products_view');
}
您是否面临任何错误?回音爆炸(“,”,$PRODRARY)
这一行完全错误。您不能像这样回显数组。请发布一个$res数组格式示例。您可以将数组转换为字符串:$arrStr=print\r(explode(“,”,$prodArray),true);echo$arrStr代码>
public function product()
{
$this->load->model('product'); // can be loaded in the parent::__construct(); at the begining of the controller
$this->products = $this->product->get_products();
$this->load->view('products_view');
}
<?php foreach ($res as $key => $value) {
$prodArray = $value->product;
if($prodArray){ foreach($prodArray as $product) {
?>
<tr class="border-bottom">
<td>
<?php echo $product; ?>
</td>
</tr>
<?php } } } ?>