Ikram Shabri
Ikram Shabri

Reputation: 567

How to unserialize data array from database to html format

I have a table in database like below

 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";}   

I want to insert that data to table in html like below

 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   

but when I tried using unsenrialize, it's just showing array value like below

Array

How do I show the data into html table ?

Here's my model code

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

}

Here's my controller code

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

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

Here's my view code

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

Upvotes: 0

Views: 247

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

change unserialize() <td> code like below:

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

Rest is good in your code

Upvotes: 2

NiniGeek
NiniGeek

Reputation: 66

Your field data in your table is an array serialized so you have to unserialize and loop over your data inside data.

One exemple:

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

Upvotes: 1

Related Questions