Skip to content
Advertisement

How to pass array/function to view page ? [OC]

Currently in my file

controller/common/home.php

$this->load->model('catalog/category');
$homepageProductTest = $this->model_catalog_category->homepageProductTest();

And when i echo $homepageProductTest , there is bunch of array.

What i wanted: How do i pass the variable to my view file (home.tpl)

The error i always get (view/theme/xxx/template/common/home.tpl)

Undefined variable: homepageProductTest

Advertisement

Answer

if you want to use any variable in your view file from controller file then you have to pass your value in data variable like below,

like in your case, you want “homepageProductTest”
$data['homepageProductTest'] = $homepageProductTest; //write this code in your controller file

OR

$this->data['homepageProductTest'] = $homepageProductTest; //write this code in your controller

in .tpl file you can use this variable using PHP like,

<?php foreach($homepageProductTest as $producttest){ ?>
  //your code here
<?php } ?>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement