My approach maybe wrong or incorrect but as it stands:
From one query, I’m looping through passing the values into a function which then uses those values to run another query and then processes this to create elements for each. So first query retrieves product categories (Traditional Favourites, Starters, Mains and Subcatgory) passed into function which retrieves the subcategory and sets up the page
for the category
[code]
$categorytitle = $connect->query(“SELECT Category_CategoryTitle, SubCategory_SubCategoryTitle, SubCategoryDescription_LrgDesc FROM lkuTbl_Category LEFT JOIN lkuTbl_SubCategory ON FK_CategoryId = PK_CategoryId”);
while($catsubcat_title = $categorytitle->fetch_assoc()) {
$categorytitles[$catsubcat_title[‘Category_CategoryTitle’]][‘SubCategoryTitles’][] = array(
‘subcategory_title’ => $catsubcat_title[‘SubCategory_SubCategoryTitle’],
‘subcategory_description’ => $catsubcat_title[‘SubCategoryDescription_LrgDesc’]
);
}
// while($catsubcat_title = $categorytitle->fetch_assoc())
foreach($categorytitles as $k => $arr) {
if($k == ‘Traditional Favourites’ || $k == ‘Starters’ || $k == ‘Mains’ ) {
createPageContentFromArg($k);
}
else {
foreach($arr as $sub => $subvl) {
createPageContentFromArg($sub);
}
}
}
// foreach($categorytitles as $k => $arr)
and then the function structure being:
[code]
function createPageContentFromArg(&$categorysubcattitle) {
require ‘dbconfig.php’;
$gbpformat = new NumberFormatter(‘en_GB’, NumberFormatter::CURRENCY);
$categorysubcategory = $connect->query(“SELECT MenuItemTitle, MenuItemDescription, MenuItemType, MenuItemPrice, Category_CategoryTitle, lkuTbl_SpecCategory_Title, SubCategory_SubCategoryTitle, SubCategoryDescription_LrgDesc FROM tblMenuItem LEFT JOIN lkuTbl_Category ON FK_CategoryId = lkuTbl_Category_PK_CategoryId LEFT JOIN lkuTbl_PriyaSpecial ON FK_SpecCategoryId = lkuTbl_SpecCategory_PK_SpecCategoryId LEFT JOIN lkuTbl_SubCategory ON FK_SubCategoryId = lkuTbl_SubCategory_PK_SubCategoryId WHERE Category_CategoryTitle = ‘$categorysubcattitle’ OR SubCategory_SubCategoryTitle = ‘$categorysubcattitle'”);
while($catsubcat_descripttitle_dishtypetitleprice = $categorysubcategory_dishtypetitleprice->fetch_assoc()) {
$traditfavourites[$catsubcat_descripttitle_dishtypetitleprice[‘Category_CategoryTitle’]][$catsubcat_descripttitle_dishtypetitleprice[‘SubCategory_SubCategoryTitle’]][$catsubcat_descripttitle_dishtypetitleprice[‘SubCategoryDescription_LrgDesc’]][$catsubcat_descripttitle_dishtypetitleprice[‘MenuItemTitle’]][] = array(
‘dish_type’ => $catsubcat_descripttitle_dishtypetitleprice[‘MenuItemType’],
‘dish_price’ => $catsubcat_descripttitle_dishtypetitleprice[‘MenuItemPrice’]
);
$mains_dishes[$catsubcat_descripttitle_dishtypetitleprice[‘MenuItemTitle’]][] = array(‘type’ => $catsubcat_descripttitle_dishtypetitleprice[‘MenuItemType’], ‘price’ => $catsubcat_descripttitle_dishtypetitleprice[‘MenuItemPrice’]);
$categorysubcategory[$catsubcat_descripttitle_dishtypetitleprice[‘SubCategory_SubCategoryTitle’]][$catsubcat_descripttitle_dishtypetitleprice[‘SubCategoryDescription_LrgDesc’]][$catsubcat_descripttitle_dishtypetitleprice[‘MenuItemTitle’]][$catsubcat_descripttitle_dishtypetitleprice[‘MenuItemDescription’]][] = array(
‘dish_type’ => $catsubcat_descripttitle_dishtypetitleprice[‘MenuItemType’],
‘dish_price’ => $catsubcat_descripttitle_dishtypetitleprice[‘MenuItemPrice’]
);
}
// while($dishtypetitleprice = $categorysubcategory_dishtypetitleprice->fetch_assoc())
if($categorysubcattitle == ‘Traditional Favorites’) {} // uses $traditfavourites and works
else if($categorysubcattitle == ‘Starters’) {} // have not scripted yet / no associative array as of yet
else if($categorysubcattitle == ‘Mains’) {} // uses $mains_dishes and works
else {} // but when I try to loop or print_r on $categorysubcategory I get the error PHP Notice: Undefined variable: categorysubcategory
Can someone help resolve this. I have checked all the key but I can’t figure out why I’m able to loop through the first two associative arrays but can with this one