I am using CodeIgniter framework. I am sending an array to my view, but I could not get the array in my view.
This is my controller code:
public function edit($id)
{
$record = $this->Start_model->get_entry($id);//receiving array from model
$this->load->view('edit',$record);//send array to my view
}
This is my array on controller that I send:
Array
(
[0] => Array
( [id] => 1 [name] => Hamza [age] => 20 [address] => audit and account [phone] => 03000000000 )
)
But when I call this array view I get this error:
Undefined variable: record
This is how I am getting my array in view:
<?php
echo '<pre>';
print_r($record);
echo '</pre>';
?>
Now I know I am sending an array to my view but I want to know If there is array in my view or not. I can get record through another method but I think it is not a good practice. So anyone can help me how I can detect if there is an array in my view?
Codeigniter extracts the array passed to a view, creating variables based on the keys of the array. To work as you want, pass an array with a key or record
and a value of your array:
public function edit($id)
{
$data = array('record' => $this->Start_model->get_entry($id));
$this->load->view('edit',$data);//send array to my view
}
Then this will work in your view:
<?php
echo '<pre>';
print_r($record);
echo '</pre>';
?>
The way you are currently sending the data, it will be extracted into individual variables for each element in the array, however as your array is numerically indexed, and php variable name rules prevent numeric variable names, you cannot access the data.
In your controller, send a parent array instead:
public function edit($id)
{
$data = array();
$data['record'] = $this->Start_model->get_entry($id); // provided this is not empty
$this->load->view('edit', $data);
}
Then in your view:
foreach($record[0] as $key => $value) {
echo $value['id'];
// the rest blah blah
}