【问题标题】:How to unserialize data array from database to html format如何将数据数组从数据库反序列化为 html 格式
【发布时间】:2019-11-24 22:15:29
【问题描述】:

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

 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>

【问题讨论】:

  • :- var_dump(unserialize($notif-&gt;additional_data)); 的输出是什么?向我们展示。你想如何在表格中表示它?也告诉我们
  • 嗨@AnantSingh---AlivetoDie 它显示的数组值类似于这个数组(1) { [0]=> string(10) "INV-000001" }
  • 那么你想通过这个数组显示什么输出。将其添加到您的问题中
  • 在名为data 的数据库表字段中,视图中additional_data - 是否正确?
  • 为什么要以序列化的方式存储这种数据?为什么不以您希望输出的方式存储它,例如Total Goals : 6250000 而不是 a:5:{i:0;s:28:"Total Goalsi:1;s:7:"6250000";}

标签: php html mysql codeigniter serialization


【解决方案1】:

更改unserialize() &lt;td&gt; 代码如下:

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

在你的代码中休息很好

【讨论】:

  • 如果我想再添加一个内爆,它可以只用 : 然后内爆?
  • 当然,取决于数据有多个值或单个值
  • @Alive 如果您有包含 1 个元素的 $data 并且内爆胶是 :,您认为您会看到多少个 :?请重新考虑这个 sn-p。
【解决方案2】:

您的表中的字段 data 是一个序列化数组,因此您必须在 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>

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2015-07-09
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    相关资源
    最近更新 更多