How to access a variable codeigniter array

Asked

Viewed 868 times

3

I have trouble picking up values within an array variable in codeigniter help me understand how this works once and for all I searched the database that is sent to the view by an array: Controller

$dados= array(
'perfil' => $this->perfil->perfil($id)
);

In the view I have the data that came from the controller. I check my $profile variable with var_dump($perfil) and it turns up:

Array
(
[0] => stdClass Object
(
[artigo_id] => 2
[artigo_titulo] => A volta de cristo
[artigo_imagem] => 8f5364fb6f5204224529ce3345073906.jpg
[artigo_autor] => Nativo Natan Melo
)
)

How do I get the article author index information? I try to access by doing

$perfil->artigo_autor;

More of a mistake :

Trying to get Property of non-object.

How should I proceed?

  • Suffice $perfil[0]->artigo_autor

  • There is no way I can access type like this; $profile->artigo_author ?

  • 1

    The return of the method has to be row

  • http://stackoverflow.com/questions/6601236/codeigniter-return-result-and-row

1 answer

2


If the method $this->perfil->perfil($id) should return only a single record change its return from result() or result_array() for row() so the zero index is not created and it is possible to directly access the properties of the object.

Change example:

$query = $this->db->query("YOUR QUERY");
return $query->result();

To:

$query = $this->db->query("YOUR QUERY");
return $query->row();

Recommended reading:

Documentation - Row()

  • Perfect, never forget. Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.