繁体   English   中英

如何将数据库中的数据数组反序列化为html格式

[英]How to unserialize data array from database to html format

我在数据库中有一个表,如下所示

 id |     touserid      |            data                 
  1         2                 a:1:{i:0;s:10:"INV-000001";}                    
  2         2                 a:1:{i:0;s:10:"INV-000003";}                  
  3         2                 a:1:{i:0;s:15:"The Mej Hotel";}                    
  4         1             a:5:{i:0;s:28:"Total Goalsi:1;s:7:"6250000";}           
  5         1                 a:1:{i:0;s:10:"INV-000007";}   

我想将数据插入html表格中,如下所示

 id |     touserid      |            data                 
  1         2                      INV-000001                    
  2         2                      INV-000003                   
  3         2                     The Mej Hotel                    
  4         1                   Total Goals : 6250000           
  5         1                     INV-000007   

但是当我尝试使用unsenrialize时,它只是显示如下数组值

Array

如何将数据显示到html表中?

这是我的模型代码

public function getAllNotifications()
{
    return $this->db->get('tblnotifications');

}

这是我的控制器代码

$data['notifications'] = $this->Sales_model->getAllNotifications()->result();

    $this->load->view('admin/sales/sales', $data);

这是我的查看代码

 <table class="table table-dark">
  <tbody>
     <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">To ID</th>
          <th scope="col">Notification</th>
        </tr>
     </thead>
     <tbody>
     <?php foreach($notifications as $notif){ ?>
        <tr>
          <td><?php echo $notif->id ?></td>
          <td><?php echo $notif->touserid ?></td>
          <td><?php echo unserialize($notif->data) ?></td>
         </tr>
      <?php } ?>
    </tbody>
  </table>

更改unserialize() <td>代码,如下所示:

<td>
  <?php 
    $data = unserialize($notif->data)
    echo (count($data) > 1) ? implode(' : ', $data) : implode('', $data); ?>
</td>

休息对您的代码有益

表中的字段数据是序列化的数组,因此您必须反序列化并循环遍历data内部的数据

一个例子:

    <table class="table table-dark">
    <tbody>
     <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">To ID</th>
          <th scope="col">Notification</th>
        </tr>
     </thead>
     <tbody>
     <?php foreach($notifications as $notif){ ?>
        <tr>
          <td><?php echo $notif->id ?></td>
          <td><?php echo $notif->touserid ?></td>
          <td><?php echo implode(',', unserialize($notif->data)) ?></td>
         </tr>
      <?php } ?>
    </tbody>
    </table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM