CodeIgniter Forums
foreach with different value results is correct but i got error message - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: foreach with different value results is correct but i got error message (/thread-71868.html)



foreach with different value results is correct but i got error message - DELE - 10-03-2018

Help me please.

Results from print_r($response->results)
Code:
[13] => stdClass Object ( [id] => 738573 [title] => Operation Finale )
[14] => stdClass Object ( [id] => 442249 [title] => The First Purge )
[15] => stdClass Object ( [id] => 79696 [name] => Manifest )

my view
Code:
<? foreach ($response->results as $v){
    $retVal = ($v->title) ? $v->title : $v->name ;
    echo $retval;
<? } ?>

If i use code above, results is correct but i get error message: Undefined property: stdClass::$title.
i know that is uncorrect because foreach for array [15] is false (not results).

how i can fix it.


RE: foreach with different value results is correct but i got error message - Piotr - 10-03-2018

isset or property_exists ?


RE: foreach with different value results is correct but i got error message - dave friend - 10-03-2018

(10-03-2018, 11:08 AM)DELE Wrote: Help me please.

Results from print_r($response->results)
Code:
[13] => stdClass Object ( [id] => 738573 [title] => Operation Finale )
[14] => stdClass Object ( [id] => 442249 [title] => The First Purge )
[15] => stdClass Object ( [id] => 79696 [name] => Manifest )

my view
Code:
<? foreach ($response->results as $v){
    $retVal = ($v->title) ? $v->title : $v->name ;
    echo $retval;
<? } ?>

If i use code above, results is correct but i get error message: Undefined property: stdClass::$title.
i know that is uncorrect because foreach for array [15] is false (not results).

how i can fix it.

If you are completely sure that $v will always have a "name" property if there is no "title" then try this.

PHP Code:
foreach ($response->results as $v){
 
   echo  isset($v->title) ? $v->title $v->name ;




RE: foreach with different value results is correct but i got error message - DELE - 10-03-2018

(10-03-2018, 04:22 PM)dave friend Wrote:
(10-03-2018, 11:08 AM)DELE Wrote: Help me please.

Results from print_r($response->results)
Code:
[13] => stdClass Object ( [id] => 738573 [title] => Operation Finale )
[14] => stdClass Object ( [id] => 442249 [title] => The First Purge )
[15] => stdClass Object ( [id] => 79696 [name] => Manifest )

my view
Code:
<? foreach ($response->results as $v){
    $retVal = ($v->title) ? $v->title : $v->name ;
    echo $retval;
<? } ?>

If i use code above, results is correct but i get error message: Undefined property: stdClass::$title.
i know that is uncorrect because foreach for array [15] is false (not results).

how i can fix it.

If you are completely sure that $v will always have a "name" property if there is no "title" then try this.

PHP Code:
foreach ($response->results as $v){
 
   echo  isset($v->title) ? $v->title $v->name ;


[SOLVED]

Thank you master.


RE: foreach with different value results is correct but i got error message - DELE - 10-03-2018

(10-03-2018, 12:08 PM)Piotr Wrote: isset or property_exists ?

for now i use isset. because if i use property_exists there is still an error message.

can you give me an example of using property_exists


RE: foreach with different value results is correct but i got error message - Piotr - 10-03-2018

If you have error then use isset(). Looks like property_exists() was just bad idea. I wrote this as quick reply without testing.