Reputation: 1883
I want to get the array from controller to views which is dynamic.
Suppose,
$a = array(array('category_id'=>'3', 'name'=>'car'),array('category_id'=> '5', 'name'=>'bike'));
$data['category'] = $a;
foreach($a as $val){
$data[$val['name']] = $this->home_model->get_product($val['category_id']);
}
$this->load->view('main',$data);
foreach($category as $val){
foreach($val['name'] as $value){ //undefined variable passed to foreach, But when I var_dump variable are printed in controller but cannot get in views. How to map the value, to value that is passed from controller.
}
}
Now the $data array in controller will be:
array (size=6)
'Car' =>
array (size=1)
0 =>
object(stdClass)[31]
public 'frontend_id' => string '3' (length=1)
public 'sub_category_id' => string '3' (length=1)
public 'category_id' => string '3' (length=1)
public 'name' => string 'Car' (length=3)
public 'slug' => string 'car' (length=3)
public 'parent_id' => string '1' (length=1)
public 'description' => string '' (length=0)
public 'status' => string '1' (length=1)
When I loop through $category
in the view then, I can get car value as
foreach($category as $val) {
$name = $val['name']; //prints car
foreach($name as $value) { //how to map $name to the 'car' array that is passed from controller. It say invalid argument supplied for foreach
}
}
Upvotes: 1
Views: 1040
Reputation: 38670
I'h tested code with your data. Its fine. So this should work
$a = array(
array('category_id'=>'3', 'name'=>'car'),
array('category_id'=> '5', 'name'=>'bike')
);
$data['category'] = $a;
foreach($a as $val)
{
$name = $val['name'];
// $category_id = $this->home_model->get_product($val['category_id']);
$category_id = $val['category_id']+5;
$NewVal[] = array(
'name' => $name,
'category_id' => $category_id
);
}
print_r($NewVal);
Output
Array ( [0] => Array ( [name] => car [category_id] => 8 ) [1] => Array ( [name] => bike [category_id] => 10 ) )
In Controller
$a = array(
array('category_id'=>'3', 'name'=>'car'),
array('category_id'=> '5', 'name'=>'bike')
);
$data['category'] = $a;
foreach($a as $val)
{
$name = $val['name'];
$category_id = $this->home_model->get_product($val['category_id']);
$NewVal[] = array(
'name' => $name,
'category_id' => $category_id
);
}
$data['category_new'] = $NewVal;
In View
foreach ($categorys_new as $category) {
print_r($category);
echo "<br>";
}
Upvotes: 0
Reputation: 2993
add $
before $val['name']
. See below code
On your View
foreach($category as $val)
{
foreach($$val['name'] as $value){
}
}
Upvotes: 1
Reputation: 6539
Your controller should be:-
$res= $data = [];
$a = array(array('category_id'=>'3', 'name'=>'car'),array('category_id'=> '5', 'name'=>'bike'));
foreach($a as $val){
$res[$val['name']] = $this->home_model->get_product($val['category_id']);
}
$data['category'] = $res;
$this->load->view('main',$data);
Your view should be:-
foreach($data['category'] as $val){
foreach($val['name'] as $value){
// do your stuff
}
}
Upvotes: 0
Reputation: 7159
Array $a
should be like,
$a[0] = array('category_id'=>'3', 'name'=>'car');
$a[1] = array('category_id'=>'5', 'name'=>'bike');
$data['category'] = $a;
Try this
Upvotes: 0