1

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

10
  • 1
    Have you ever read the codeigniter select manual? Commented Oct 12, 2022 at 11:13
  • I am new to codeigniter. I managed to solve this in laravel by using "json_decode" . But In codeIgniter it's not working Commented Oct 12, 2022 at 11:18
  • Why wouldn't json_decode work in CI? What code did you use? Do you get an error? If so, what does it say? Commented Oct 12, 2022 at 11:21
  • I have updated the question. the error is-- json_decode() expects parameter 1 to be string, array given Commented Oct 12, 2022 at 11:23
  • Assuming $row->caption contains your JSON you'd need to json_decode($row->caption) Commented Oct 12, 2022 at 11:27

1 Answer 1

2

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;
        }
    }
    ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Trying to get property 'data' of non-object--- showing on foreach loop
I have changed the code. now its working

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.