I've run into a problem when building any array that holds my menu category and products, I’ve got the main category and the products for them categories into my array fine its when I try to get sub categories products into the array I cant seem to work out.
Below is a set of arrays with dummy values(my website has a category table and a product table in the data base)
$cats = array( 0 => array( 'categoryID' => 1, 'name' => 'Menu', 'parent' => 0)); //root of the category
$categorys = array( 1 => array(
1 => array( 'categoryID' => 6 , 'name' => 'Breads' , 'parent' => 1 )
,0 => array( 'categoryID' => 2 , 'name' => 'Drinks' , 'parent' => 1 )
,2 => array( 'categoryID' => 7 , 'name' => 'Salads' , 'parent' => 1 )
)
,2 => array(
0 => array( 'categoryID' => 4 , 'name' => 'Tea' , 'parent' => 2 )
,1 => array( 'categoryID' => 18 , 'name' => 'Coffee' , 'parent' => 2 )
)
); // the category themselfs
$product = array(6 => array(
0 => array('productID' => 1, 'categoryID' => 6,'name' => 'Plain Bread', 'description' => 'description')
,1 => array('productID' => 2, 'categoryID' => 6, 'name' => 'Garlic Bread', 'description' => 'description')
)
,7 => array(
0 => array('productID' => 44, 'categoryID' => 7, 'name' => 'Garden Salad', 'description' => 'description')
,1 => array('productID' => 45, 'categoryID' => 7, 'name' => 'Greek Salad', 'description' => 'description')
)
,4 => array(
0 => array('productID' => 74, 'categoryID' => 4, 'name' => 'Green Tea', 'description' => 'description')
)
,18 => array(
0 => array('productID' => 75, 'categoryID' => 18, 'name' => 'Espresso', 'description' => 'description')
)
);
below is my code that makes the array
if(is_array($cats)) {
foreach($cats as $cat) {
if(is_array($categorys[$cat['categoryID']])) {
foreach($categorys[$cat['categoryID']] as $category) {
$category['product'] = $product[$category['categoryID']]; //makes the product array and adds any products that have the same categoryID as the category
$category['subcategory'] = $categorys[$category['categoryID']];
$this_category[] = $category;
}
}
}
if(!empty($this_category))
{
$ncategory[] = array($cat,$this_category);
}
$this_category = array();
}
print_r($ncategory);
below is as far so I got to trying to work it out
if(is_array($cats)) {
foreach($cats as $cat) {
if(is_array($categorys[$cat['categoryID']])) {
foreach($categorys[$cat['categoryID']] as $category) {
$category['product'] = $product[$category['categoryID']]; //makes the product array and adds any products that have the same categoryID as the category
$category['subcategory'] = $categorys[$category['categoryID']];
$this_category[] = $category;
if(is_array($category['subcategory'])) {
foreach($category['subcategory'] as $sub) {
$sub['product'] = $product[$sub['categoryID']];
$this_prod[] = $sub;
}
}
}
}
}
if(!empty($this_category))
{
$ncategory[] = array($cat,$this_category);
}
$this_category = array();
}
I know $this_prod isn’t doing anything I’m just not sure what to do with it
thank you for your time sorry for any spelling or grammar errors