如何从数据库爆炸逗号分隔的数组

时间:2019-03-23 11:56:23

标签: php codeigniter

我无法在Codeigniter框架中从数据库爆炸逗号分隔的数据数组。我想用字符串值将数组回显为多行,例如:

---------------
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中爆炸并获取数据

2 个答案:

答案 0 :(得分:1)

查看文件

   <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 get_products()
{
    $this->db->select('*');
    $this->db->from('products_table'); //update with your table name
    return $this->db->get()->result_object(); // sucess result or handle exceptiom here
}

控制器文件

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');
}

答案 1 :(得分:0)

<?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 } } } ?>