I'm trying to pass two arrays from controller to view, using this approach:
My underlying data query has extracted this data as follows: $catalogData: Title (Clothing); Season (Winter); $ProductData: Type (Shirts); Size (XL); Price ($10);
Controller
$this->load->view('users/TheView', $catalogData, $productData);
View
<?php
echo $Catalog;
echo $Season;
echo $Type;
echo $Size;
echo $Price;
?>
My error message is
A PHP Error was encountered
Severity: Notice
Message: Undefined index: Catalog
Filename: users/controller.php
I cant seem to find any examples of passing two arrays to a view, which makes me think it's not possible?
Edit: I'm using CodeIgniter
Without knowing anything about the MVC framework in question, I can only assume it expects one array argument after the view name and uses extract
, in which case I'd do the following
$this->load->view('users/TheView', array(
'catalog' => $catalogData,
'product' => $productData));
and in your view...
<?php
echo $catalog['Title'], $catalog['Season'], $product['Type'], etc
Also, your error message seems to indicate that you should be using $Title
instead of $Catalog
. Title
is the property name shown in your data example.
A couple different thing I can think of,
First: you are trying to echo $Catalog, but I don't see anywhere that you are passing a variable with that name to your view. This leads to the second part.
Second: You need to echo out the object you are trying to print, not the Array. so they should be something like:
<?php echo $catalogData['Title']; ?>
Finally, I honestly never do php programing without a framework, and you don;t mention if you are using one, but typically in those you pass an array of variables into your view. So something like:
$this->load->view('users/TheView', array('catalogData'=>$catalogData, 'productData'=>$productData);
First thing is if you want to pass more than one array to view then you should do something like this in controller
$data=array();
$data['catalogData']=$this->model_name->function_name(); // query for catalog
$data['ProductData']=$this->model_name->function_name(); // query for product
$this->load->view('view_name',$data);
in view
if(isset($catalogData) && is_array($catalogData) && count($catalogData)>0)
{
echo $catalogData['Title'];
}
these is the procedure.Please let me know if you face any problem.
You can pass two arrays like this
$data['catalogData'] = $catalogData;
$data['productData'] = $productData;
$this->load->view('users/TheView', $data);