0

My DB has 2 rows of values : Test2 campaign & premium dove

view page

foreach ($estedit as $estrow) { 
                 //print_r($estrow);
                 echo $estrow->name;
                              }

The above code shows these 2 values at the bottom as in the below linked image, but it displays error Trying to get property 'name' of non-object.

Error Screenshot:

https://i.sstatic.net/g7wtz.png

print_r($estrow); inside the foreach loop shows the below given data:

stdClass Object ( [est_id] => 3 [name] => Test2 campaign ) stdClass Object ( [est_id] => 1 [name] => premium dove ).

Accessing the name field as $estrow->name shows the error Illegal string offset 'name'. What am i doing wrong? How can i access the name field here ?

Controller

 $estedit[] = '';

                    foreach ($est_id as $item) {

                        $estedit[] = $this->Report->get_estedit($item);

                        $estlineedit[] = $this->Report->get_estline_edit($item);
                    }
                    $data['estedit'] = $estedit;
                    $data['estlineedit'] = $estlineedit;
                    $data['date'] = $date;
                    $this->load->view('reports/download_report_view', $data);
5
  • I belive you are reassign $estedit somewhere inside foreach loop Commented Dec 9, 2021 at 7:28
  • @Anton No. i didn't Commented Dec 9, 2021 at 7:54
  • @Anton see the update dquestion body. i've added my controller code portion from where am passing this data to view Commented Dec 9, 2021 at 7:57
  • It makes no sense that this would show the values you are after in the first place, if it could not access them. "print_r($estrow); inside the foreach loop shows the below given data" - okay, but that does not necessarily mean, that the error comes from the same loop iterations, in which you are getting this debug output. print_r(null) or print_r(false) for example result in absolutely no visible output - so if you had more items in $estedit than you thought maybe, and one of them was null or false, that would absolutely explain what you are seeing. Commented Dec 9, 2021 at 8:16
  • Use var_dump instead of print_r, that is a better debug tool. And use it to check what $estedit contains, instead of trying to figure out what is going on only inside the loop. Commented Dec 9, 2021 at 8:16

2 Answers 2

1

You set empty string as first element of $estedit

 $estedit[] = '';

Looks like you want

 $estedit = [];

instead

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

Comments

0

Use this:

$estedit = array(); 

Or this:

$estedit = []; 

Instead of:

$estedit[] = '';

Comments

Your Answer

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