PHP Codeigniter: $data variables available to view in print_r but direct access gives 'undefined index' error
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
Answer
Solution:
Try this one. if your array is like this in controller
then in view you may access it like this.
Or if it is multiple record's array you will use
Answer
Solution:
I think that's syntax error, did you mean:
and not:
Answer
Solution:
I suspect this will give you what you want
Answer
Solution:
It's easy to miss, but the following will work.
When you did a var_dump, I noticed that there was an array index.