0

I have a table (tb_proxmox) with 2 field (proxmox_id, proxmox_data). My record in field proxmox_data (this dummy record)

{"ip0_proxmox":"31231","ip1_proxmox":"31312313","ip2_proxmox":"","nama_proxmox":"31","port_proxmox":"3131","user_proxmox":"","pwd_proxmox":"","type_proxmox":"","ram_proxmox":"","storage_proxmox":""}

My function in my controller

    $data = array(
        'ambil_data' => $this->M_proxmox->tampilData('tb_proxmox'), 
        );      

My function in my model (M_proxmox)

    public function tampilData($table){
    return $this->db->get($table)->result();
  }

In my View

              <tbody>
              <?php
                $no = 1;
                foreach ($ambil_data as $row) {
                $datas = json_decode($row->proxmox_data,true);
                foreach($datas as $datum){
              ?>
                <tr>
                  <td><?php echo $no++ ;?></td>
                  <td><?php echo $row->proxmox_id; ?></td>
                  <td><?php echo $datum['ip1_proxmox'];?></td>

                  <td><a href="#" class="fa fa-pencil"></a><a href="#" class="fa fa-remove"></a></td>
                </tr>
              </tbody>
              <?php
                  }
                }
                ?>

I got this error Illegal string offset 'ip1_proxmox'

1 Answer 1

0

You've got this error, because you are iterating through decoded json and $datum variable contains only values, not keys.

If you want to get data from json this way, you have to change your view:

<tbody>
<?php
    $no = 1;
    foreach ($ambil_data as $row) {
        $datas = json_decode($row->proxmox_data,true);
?>
    <tr>
        <td><?php echo $no++ ;?></td>
        <td><?php echo $row->proxmox_id; ?></td>
        <td><?php echo $datas['ip1_proxmox'];?></td>

        <td><a href="#" class="fa fa-pencil"></a><a href="#" class="fa fa-remove"></a></td>
    </tr>
<?php
    }
?>
</tbody>

I also moved </tbody> outside foreach.

Sign up to request clarification or add additional context in comments.

1 Comment

you're right! thank you. I've seen from accepted answer that use 2 foreach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.