Home > Net >  accessing array of objects in codeigniter
accessing array of objects in codeigniter

Time:10-12

I am trying to show datas from database. But the datas are showing as they appear in my database.

like this--

{"caption":"hello","link":"","medias":["66daf3ea.jpg"],"advance":"null"}
{"caption":"hi","link":"","medias":null,"advance":"null"}
{"caption":"nice","link":"https:\/\/www.youtube.com\/watch?v=U10nBuERNIA&list=RDd-DwZmU1-pQ&index=24","medias":null,"advance":"null"}

I just want to show the "caption"

In my view file. the code is--

<?php
    foreach (json_decode($h->result())->data as $row) {
        if ($row->social_network == 'twitter' ) {
            echo $row->caption ;
        }
      }
    ?>

my controller code is--

public function fb_posts()
{
    $this->load->database();  
    $this->load->model('select');  
    $data['h']=$this->select->select();  
    $this->load->view('fb_posts', $data);  
}

How do I process the data and show them separately?

If I use json_decode it's showing " json_decode() expects parameter 1 to be string, array given"

database image

CodePudding user response:

From the pic you post, I can see the caption is stored in a JSON string. So, in order to use the caption field, you need to decode the data first.

Check out the code below:

<?php
     foreach ($h->result() as $row) {
        if ($row->social_network == 'twitter') {
            $data = json_decode($row->data);
            echo $data->caption;
        }
    }
    ?>
  • Related