In this codeigniter model, I am trying to push the category name of each product. I want the array to look like:
Array
(
[0] => Array
(
[id] => 1
[name] => Game 1!
[category_id] => 3
[category] => games //the category element is in the [0] array.
)
[1] => Array
(
[id] => 2
[name] => Game 2
[category_id] => 3
[category] => games
)
here's how the array looks right now:
Array
(
[0] => Array
(
[category] => games //i want this to be in the [1] array
)
[1] => Array
(
[id] => 1
[name] => Game 1!
[category_id] => 3
)
[2] => Array
(
[category] => games
)
[3] => Array
(
[id] => 2
[name] => Game 2
[category_id] => 3
)
here's my function that gets all the products and puts them in the array.
function getAllProducts(){
$data=array();
$Q=$this->db->get('products');
if($Q->num_rows() > 0){
foreach($Q->result_array() as $row){
$Q2=$this->db->query("select name FROM categories
where id={$row['category_id']}");
if($Q2->num_rows() > 0){
foreach($Q2->result_array() as $row2){
//Trouble Here: dont know how to push this into the array.
//is there a function that i can put in the $data[___]
//area so that it knows it is in the [0] element, then [1],etc?
$data[]['category']=$row2['name'];
}
}
$data[]=$row;
}
}
$Q->free_result();
return $data;
}