Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

accessing array of objects in codeigniter

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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;
        }
    }
    ?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading