CodeIgniter Forums

Full Version: Array values retrieving error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jenie_net[/eluser]
Hi all, i'm new.

I've a strange error, probably done by mine in-experience.
I've got a model
Code:
<?php
class Contacts_model extends Model {


    function Contacts_model()

    {

        parent::Model();

    }

    function getContactDetails($id){
        $this->db->where('c_id', $id);
        $rset=$this->db->get('contacts');
        return $rset->result(); // or should i return $rset? or $rset->result_array();
    }

}
?>

A simple controller:
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    

    function details($id)
    {
        $this->_default();
        $data['det'] = $this->Contacts_model->getContactDetails($id);    
        $this->load->view('details',$data);
        $this->_footer();
    }

    function _default(){
        $h_data['base'] = $this->config->item('base_url');
        $h_data['css'] = $this->config->item('css');    
        $this->load->model('Contacts_model');
        $this->load->view('header',$h_data);    
    }

    function _footer(){
        $this->load->view('footer');
    }

}
?>

How can i print in a view the values in $data['det'] ?
I tried with :

Code:
<?php print_r($det); ?>


and is OK, but if i try to retrieve values
in this way

Code:
<?php echo $det['location']; ?>

I've got this error:

Quote:#
A PHP Error was encountered

Severity: Notice

Message: Undefined index: location <--- why undefined index???

Filename: views/details.php

Line Number: 11

Thanks in advance.

Stef

El Forum

[eluser]wiredesignz[/eluser]
result() is an object, $det->location will work. result_array() returns an array for you to use $det['location'].

El Forum

[eluser]jenie_net[/eluser]
Ok, you are right, but in that case :

Code:
&lt;?php echo $det->location; ?&gt;

i've got this error:

Quote:#
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/details.php

Line Number: 7

I can't believe! :bug:

Stef

El Forum

[eluser]mironcho[/eluser]
Hi,
result() returns array of objects (database records), so you have to access it by index:
Code:
&lt;?php echo $det[0]->location; ?&gt;
or if using result_array():
Code:
&lt;?php echo $det[0]['location']; ?&gt;

El Forum

[eluser]jenie_net[/eluser]
Hi, thanks a lot for your help!
Now all is going right! :-)

Stef