PHP Codeigniter: $data variables available to view in print_r but direct access gives 'undefined index' error

835

See last code block for controller code. When I use the line below in my view, it returns all the variables in the property array:

<?php echo print_r($property);?>

However, when I want to access just one item from the $property array, I get an undefined index error:

<?php echo print_r($property['county']);?> 

Here is a var_dump ($property)

array (size=1)
  0 => 
    array (size=6)
      'property_id' => string '1' (length=1)
      'address1' => string '26 College Green Walk' (length=21)
      'town_city' => string 'Derby' (length=5)
      'county' => string 'Derbyshire' (length=10)
      'property_description' => string '<p>
    This is a property description for a rental property in Mickleover. Very nice it is too!</p>
' (length=100)
      'property_images_filepath' => string 'ee736-6.jpg' (length=11) 

Can anyone point where to look because I'm stumped!?

The controller:

public function __construct()
{
    parent::__construct();
    $this->load->model('agency_model');


    $this->output->enable_profiler(TRUE);
}



/**
 * Shows the details of the property specified by the $id parameter
 * @param string $id the id of the property to be retrieved from the DB
 */
public function show () {


$id = $this->input->get('property_id');


    if (!isset($id)) 
    {

        // Whoops, we don't have a page for that!
        show_404();
    }


    else

    {

        $data['property'] = $this->agency_model->get_property_details($id);
        $data['title'] = 'Current Properties';

        $this->load->view('templates/header', $data);
        $this->load->view('agency/property_details_view', $data);
        $this->load->view('templates/footer');
    }

}

}

Thanks Gareth

479

Answer

Solution:

Try this one. if your array is like this in controller

$data['info'] = array(
    'col_1' => "first_1",
     'col_2'=>  "first_2"
);
$this->load->view('folder/page',$data);

then in view you may access it like this.

echo $info['col_1'];
echo $info['col_1'];

Or if it is multiple record's array you will use

foreach($info as $row)
{
 //your code/statements.
}
823

Answer

Solution:

I think that's syntax error, did you mean:

<?php echo print_r($property['country');?>  

and not:

<?php echo print_r($property['county');?>  
483

Answer

Solution:

I suspect this will give you what you want

 foreach ($property as $properties)
     print_r($properties['county']);
 endforeach;
141

Answer

Solution:

It's easy to miss, but the following will work.

<?php echo print_r( $property[ 0 ]['county'] ); ?> 

When you did a var_dump, I noticed that there was an array index.

People are also looking for solutions to the problem: html - using a php file as a img src

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.