如何将json值数据库转换为html

时间:2019-07-16 03:22:37

标签: php html mysql json codeigniter

我的数据库上有通知表,该表使用的值类似于json。

这是我的桌子

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表中使用该值,但是我不知道如何在codeigniter中将该值转换为html表

这是我的查看代码

<table class="table table-dark">
                      <tbody>
                        <thead>
                          <tr>
                            <th scope="col">#</th>
                            <th scope="col">Data</th>
                            <
                          </tr>
                        </thead>
                        <tbody>
                          <?php foreach($notifications as $notif){ ?>
                          <tr>
                              <td><?php echo $notif['id'] ?></td>
                              <td><?php echo $notif['data'] ?></td>
                          </tr>
                          <?php } ?>
                      </tbody>
                    </table>

这是控制器代码

$this->db->limit($this->misc_model->get_notifications_limit(), $offset);
    $this->db->where('touserid', get_staff_user_id());
    $this->db->order_by('date', 'desc');
    $data['notifications'] = $this->db->get(db_prefix() . 'notifications')->result_array();

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

但是我没有看到数据值像我想要的那样进入html表,在表中显示了错误消息“ not_goal_message_failedArray”

我正在尝试对json进行编码,但我仍然不知道如何在控制器中传递json编码以在codeigniter中查看

这是json编码

$page   = $this->input->post('page');
        $offset = ($page * $this->misc_model->get_notifications_limit());
        $this->db->limit($this->misc_model->get_notifications_limit(), $offset);
        $this->db->where('touserid', get_staff_user_id());
        $this->db->order_by('date', 'desc');
        $notifications = $this->db->get(db_prefix() . 'notifications')->result_array();
        $i             = 0;
        foreach ($notifications as $notification) {
            if (($notification['fromcompany'] == null && $notification['fromuserid'] != 0) || ($notification['fromcompany'] == null && $notification['fromclientid'] != 0)) {
                if ($notification['fromuserid'] != 0) {
                    $notifications[$i]['profile_image'] = '<a href="' . admin_url('staff/profile/' . $notification['fromuserid']) . '">' . staff_profile_image($notification['fromuserid'], [
                    'staff-profile-image-small',
                    'img-circle',
                    'pull-left',
                ]) . '</a>';
                } else {
                    $notifications[$i]['profile_image'] = '<a href="' . admin_url('clients/client/' . $notification['fromclientid']) . '">
                <img class="client-profile-image-small img-circle pull-left" src="' . contact_profile_image_url($notification['fromclientid']) . '"></a>';
                }
            } else {
                $notifications[$i]['profile_image'] = '';
                $notifications[$i]['full_name']     = '';
            }
            $data = '';
            if (!empty($notification['data'])) {
                $data = unserialize($notification['data']);
                $x               = 0;
                foreach ($data as $dt) {
                    if (strpos($dt, '<lang>') !== false) {
                        $lang = get_string_between($dt, '<lang>', '</lang>');
                        $temp = _l($lang);
                        if (strpos($temp, 'project_status_') !== false) {
                            $status = get_project_status_by_id(strafter($temp, 'project_status_'));
                            $temp   = $status['name'];
                        }
                        $dt[$x] = $temp;
                    }
                    $x++;
                }
            }
            $notifications[$i]['description'] = _l($notification['description'], $dt);
            $notifications[$i]['date']        = time_ago($notification['date']);
            $notifications[$i]['full_date']   = $notification['date'];
            $i++;
        } 
        echo json_encode($notifications);

尝试将表中的json值转换为html表代码时,您知道我的错误在哪里吗?

谢谢

1 个答案:

答案 0 :(得分:0)

  1. 您表中的数据看起来像一个序列化数组
  2. 您将不会通过echo获取数据,应使用

    $ this-> load-> view(“ notification_view”,$ notifications);

代替

echo json_encode($notifications);