/    Sign up×
Community /Pin to ProfileBookmark

associative array and PHP Notice: Undefined variable

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)
[/code]

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
[/code]

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

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmNov 12.2019 — Not sure what the problem is here. Your last sentence seems to be mis-typed? And - what line does the error message point to exactly?
Copy linkTweet thisAlerts:
@nsathauthorNov 12.2019 — @ginerjm#1610809

It within the else. if I was to say to a print_r

else {

print_r($categorysubcategory);

}

its this line more specifically

Put simply I can reference/ use that associative array ($categorysubcategory) in any way
Copy linkTweet thisAlerts:
@ginerjmNov 12.2019 — Still don't understand you.


1 - where is the exact line that is giving you that message?

2 - if you "can reference/use that associative array", what is the problem? Or are you not saying it correctly?
Copy linkTweet thisAlerts:
@nsathauthorNov 12.2019 — @ginerjm#1610811 sorry I can't reference/ use the array

the error falls or comes about when I try reference/ use the array

this is the whole script

<i>
</i>
&lt;div class="menu"&gt;

&lt;div class="container"&gt;

<i> </i>&lt;?php

<i> </i> require 'dbconfig.php';

<i> </i> if($connect) {

<i> </i> ?&gt;

<i> </i> &lt;div class="menucard"&gt;

<i> </i> &lt;?php


<i> </i> $categorytitle_title;
<i> </i> $subcategorytitle_title;
<i> </i> $subcategorydesc_description;

<i> </i> $catsubcat = array();

<i> </i> $categorytitle = $connect-&gt;query("SELECT Category_CategoryTitle, SubCategory_SubCategoryTitle, SubCategoryDescription_LrgDesc FROM lkuTbl_Category LEFT JOIN lkuTbl_SubCategory ON FK_CategoryId = PK_CategoryId");
<i> </i> while($catsubcat_title = $categorytitle-&gt;fetch_assoc()) {
<i> </i> $categorytitles[$catsubcat_title['Category_CategoryTitle']]['SubCategoryTitles'][] = array(
<i> </i> 'subcategory_title' =&gt; $catsubcat_title['SubCategory_SubCategoryTitle'],
<i> </i> 'subcategory_description' =&gt; $catsubcat_title['SubCategoryDescription_LrgDesc']
<i> </i> );
<i> </i> }
<i> </i> // while($catsubcat_title = $categorytitle-&gt;fetch_assoc())

<i> </i> foreach($categorytitles as $k =&gt; $arr) {

<i> </i> if($k == 'Traditional Favourites' || $k == 'Starters' || $k == 'Mains' ) {
<i> </i>
<i> </i> createPageContentFromArg($k);
<i> </i>
<i> </i> }
<i> </i> else {
<i> </i> foreach($arr as $sub =&gt; $subvl) {
<i> </i> createPageContentFromArg($sub);
<i> </i> }
<i> </i> }
<i> </i>
<i> </i>
<i> </i> }
<i> </i> // foreach($categorytitles as $k =&gt; $arr)


<i> </i> ?&gt;

<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- // menucard --&gt;

<i> </i> &lt;?php

<i> </i> }
<i> </i> // if($connect)





<i> </i> function createPageContentFromArg(&amp;$categorysubcattitle) {

<i> </i> require 'dbconfig.php';
<i> </i> $gbpformat = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);

<i> </i> $categorysubcategory_dishtypetitleprice = $connect-&gt;query("SELECT MenuItemTitle, MenuItemDescription, MenuItemType, MenuItemPrice, Category_CategoryTitle, SpecCategory_Title, SubCategory_SubCategoryTitle, SubCategoryDescription_LrgDesc FROM tblMenuItem LEFT JOIN lkuTbl_Category ON FK_CategoryId = PK_CategoryId LEFT JOIN PriyaSpecial ON FK_SpecCategoryId = PK_SpecCategoryId LEFT JOIN lkuTbl_SubCategory ON FK_SubCategoryId = lkuTbl_SubCategory_PK_SubCategoryId WHERE Category_CategoryTitle = '$categorysubcattitle' OR SubCategory_SubCategoryTitle = '$categorysubcattitle'");
<i> </i> while($catsubcat_descripttitle_dishtypetitleprice = $categorysubcategory_dishtypetitleprice-&gt;fetch_assoc()) {

<i> </i>
<i> </i> $traditfavourites[$catsubcat_descripttitle_dishtypetitleprice['Category_CategoryTitle']][$catsubcat_descripttitle_dishtypetitleprice['SubCategory_SubCategoryTitle']][$catsubcat_descripttitle_dishtypetitleprice['SubCategoryDescription_LrgDesc']][$catsubcat_descripttitle_dishtypetitleprice['MenuItemTitle']][] = array(
<i> </i> 'dish_type' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemType'],
<i> </i> 'dish_price' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemPrice']
<i> </i> );
<i> </i>
<i> </i> $mains_dishes[$catsubcat_descripttitle_dishtypetitleprice['MenuItemTitle']][] = array('type' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemType'], 'price' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemPrice']);
<i> </i>
<i> </i> $categorysubcategory[$catsubcat_descripttitle_dishtypetitleprice['SubCategory_SubCategoryTitle']][$catsubcat_descripttitle_dishtypetitleprice['SubCategoryDescription_LrgDesc']][$catsubcat_descripttitle_dishtypetitleprice['MenuItemTitle']][$catsubcat_descripttitle_dishtypetitleprice['MenuItemDescription']][] = array(
<i> </i> 'dish_type' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemType'],
<i> </i> 'dish_price' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemPrice']
<i> </i> );
<i> </i>
<i> </i>

<i> </i> }
<i> </i> // while($dishtypetitleprice = $categorysubcategory_dishtypetitleprice-&gt;fetch_assoc())

<i> </i> if($categorysubcattitle == 'Traditional Favourites') {
<i> </i> foreach($traditfavourites as $k =&gt; $arr) {
<i> </i> $categorytitle_title = $k;
<i> </i> ?&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', "menucard-" . $categorytitle_title)); ?&gt;"&gt;
<i> </i> &lt;div class="traditionalfavouritestitle"&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"&gt;
<i> </i> &lt;h2 class="header-traditionalfavourites"&gt;
<i> </i> &lt;?php echo $categorytitle_title; ?&gt;
<i> </i> &lt;/h2&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- // traditionalfavouritestitle --&gt;
<i> </i> &lt;?php
<i> </i> foreach($arr as $subt =&gt; $ary) {
<i> </i> $subcategorytitle_title = $subt;
<i> </i> ?&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)); ?&gt;"&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-titledescription"; ?&gt;"&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-title"; ?&gt;"&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"&gt;
<i> </i> &lt;h3 class="&lt;?php echo str_replace(' ', '', strtolower($subcategorytitle_title)); ?&gt;"&gt;
<i> </i> &lt;?php echo $subcategorytitle_title; ?&gt;
<i> </i> &lt;/h3&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-title"; --&gt;
<i> </i> &lt;?php
<i> </i> foreach($ary as $desc =&gt; $ar) {
<i> </i> if($desc != NULL) {
<i> </i> $subcategorydesc_description = $desc;
<i> </i> ?&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-description"; ?&gt;"&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"&gt;
<i> </i> &lt;p class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-description"; ?&gt;"&gt;
<i> </i> &lt;?php echo $subcategorydesc_description; ?&gt;
<i> </i> &lt;/p&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-description"; --&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // if($desc != NULL)
<i> </i> }
<i> </i> // foreach($ary as $desc =&gt; $ar)
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-titledescription"; --&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-dishtypetitledishprice"; ?&gt;"&gt;
<i> </i> &lt;?php
<i> </i> foreach($ar as $title =&gt; $tp) {
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-xs-9 col-sm-10 col-lg-11"&gt;
<i> </i> &lt;h4 class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-dishtitle"; ?&gt;"&gt;
<i> </i> &lt;?php echo $title;
<i> </i> foreach($tp as $aryk =&gt; $aryv) {
<i> </i> foreach($aryv as $arykey =&gt; $aryval) {
<i> </i> if($arykey == 'dish_type') {
<i> </i> if($aryval == "Vegetarian") {
<i> </i> ?&gt;
<i> </i> &lt;span class="vegetarianicon"&gt;Ⓥ&lt;/span&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // if($aryval == "Vegetarian")
<i> </i> }
<i> </i> // if($arykey == 'dish_type')
<i> </i> }
<i> </i> // foreach($aryv as $arykey =&gt; $aryval)
<i> </i> }
<i> </i> // foreach($tp as $aryk =&gt; $aryv)
<i> </i> ?&gt;
<i> </i> &lt;/h4&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class="col-xs-9 col-sm-10 col-lg-11 --&gt;
<i> </i> &lt;?php
<i> </i> foreach($aryv as $arykey =&gt; $aryval) {
<i> </i> if($arykey != 'dish_type') {
<i> </i> ?&gt;
<i> </i> &lt;div class="col-xs-3 col-sm-2 col-lg-1"&gt;
<i> </i> &lt;p class="&lt;?php echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-dishprice"; ?&gt;"&gt;
<i> </i> &lt;?php echo $gbpformat-&gt;formatCurrency($aryval, 'GBP'), PHP_EOL; ?&gt;
<i> </i> &lt;/p&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // if($arykey != 'dish_type')
<i> </i> }
<i> </i> // foreach($aryv as $arykey =&gt; $aryval)
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class="row" --&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // foreach($ar as $title =&gt; tp)
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $subcategorytitle_title)) ."-dishtypetitledishprice"; --&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $subcategorytitle_title)); --&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', "menucard-" . $categorytitle_title)); --&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // foreach($traditfavourites as $k =&gt; $arr)
<i> </i> }
<i> </i> // if($categorysubcattitle == 'Traditional Favourites')






<i> </i>
<i> </i> else if($categorysubcattitle == 'Starters') {
<i> </i> $categorytitle_title = $categorysubcattitle;
<i> </i> ?&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $categorytitle_title)); ?&gt;"&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', "veg-" . $categorytitle_title)); ?&gt;"&gt;



<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', "veg-" . $categorytitle_title)); --&gt;

<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', "nonveg-" . $categorytitle_title)); ?&gt;"&gt;



<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', "nonveg-" . $categorytitle_title)); --&gt;

<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', "special-" . $categorytitle_title)); ?&gt;"&gt;



<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', "special-" . $categorytitle_title)); --&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $categorytitle_title)); --&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // else if($categorysubcattitle == 'Starters')







<i> </i> else if($categorysubcattitle == 'Mains') {
<i> </i> $categorytitle_title = $categorysubcattitle;
<i> </i> ?&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $categorytitle_title)); ?&gt;"&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $categorytitle_title)) ."-title"; ?&gt;"&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"&gt;
<i> </i> &lt;h2 class="&lt;?php echo strtolower(str_replace(' ', '', $categorytitle_title)); ?&gt;"&gt;
<i> </i> &lt;?php echo $categorytitle_title; ?&gt;
<i> </i> &lt;/h2&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $categorytitle_title)) ."-title"; --&gt;
<i> </i> &lt;div class="&lt;?php echo strtolower(str_replace(' ', '', $categorytitle_title)) ."-dishtypetitledishprice"; ?&gt;"&gt;
<i> </i> &lt;?php
<i> </i> foreach($mains_dishes as $h =&gt; $i) {
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-xs-9 col-sm-10 col-lg-11"&gt;
<i> </i> &lt;h4 class="&lt;?php echo strtolower(str_replace(' ', '', $categorytitle_title)) ."-dishtitle"; ?&gt;"&gt;
<i> </i> &lt;?php echo $h;
<i> </i> foreach($i as $aryk =&gt; $arkv) {
<i> </i> foreach($arkv as $arraykey =&gt; $arrayval) {
<i> </i> if($arraykey == 'type') {
<i> </i> if($arrayval == "Vegetarian") {
<i> </i> ?&gt;
<i> </i> &lt;span class="vegetarianicon"&gt;Ⓥ&lt;/span&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // if($arrayval == "Vegetarian")
<i> </i> }
<i> </i> // if($arrky == 'type')
<i> </i> }
<i> </i> // foreach($arykey as $arraykey =&gt; $arrayval)
<i> </i> }
<i> </i> // foreach($i as $aryk =&gt; $arkv)
<i> </i> ?&gt;
<i> </i> &lt;/h4&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class="col-xs-9 col-sm-10 col-lg-11 --&gt;
<i> </i> &lt;?php
<i> </i> foreach($i as $aryk =&gt; $arkv) {
<i> </i> foreach($arkv as $arraykey =&gt; $arrayval) {
<i> </i> if($arraykey != 'type') {
<i> </i> ?&gt;
<i> </i> &lt;div class="col-xs-3 col-sm-2 col-lg-1"&gt;
<i> </i> &lt;p class="&lt;?php echo strtolower(str_replace(' ', '', $categorytitle_title)) ."-dishprice"; ?&gt;"&gt;
<i> </i> &lt;?php echo $gbpformat-&gt;formatCurrency($arrayval, 'GBP'), PHP_EOL; ?&gt;
<i> </i> &lt;/p&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // if($arrky == 'type')
<i> </i> }
<i> </i> // foreach($arkv as $arraykey =&gt; $arrayval)
<i> </i> }
<i> </i> // foreach($i as $aryk =&gt; $arkv)
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class="row" --&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // foreach($mains_dishes as $h =&gt; $i)
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', $categorytitle_title)) ."-dishtypetitledishprice"; --&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- div class= echo strtolower(str_replace(' ', '', "menucard-" . $categorytitle_title)); --&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> // else if($categorysubcattitle == 'Mains')







<i> </i> else {
<i> </i>
<i> </i> print_r($categorysubcategory);
<i> </i>
<i> </i>
<i> </i>
<i> </i>
<i> </i> }



<i> </i> }
<i> </i> // end of (closing brace) function CreatePageContentFromArg(&amp;$str_title)




<i> </i>?&gt;

&lt;/div&gt;
&lt;!-- // container --&gt;

&lt;/div&gt;
&lt;!-- // menu --&gt;


from this its line 332

the: print_r($categorysubcategory);
within the else
Copy linkTweet thisAlerts:
@NogDogNov 12.2019 — Since you don't initialize that variable before this...
``<i>
</i>while($catsubcat_descripttitle_dishtypetitleprice = $categorysubcategory_dishtypetitleprice-&gt;fetch_assoc()) {<i>
</i>
``

...my guess is that you are are not getting any rows at all, so you never hit the assignment statement for that variable within that loop.
Copy linkTweet thisAlerts:
@nsathauthorNov 12.2019 — @NogDog#1610814

I did
<i>
</i>$categorysubcategory_dishtypetitleprice = $connect-&gt;query("SELECT MenuItemTitle, MenuItemDescription_LrgDesc, MenuItemType, MenuItemPrice, lkuTbl_Category_CategoryTitle, lkuTbl_SpecCategory_Title, lkuTbl_SubCategory_SubCategoryTitle, lkuTbl_SubCategory_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 lkuTbl_Category_CategoryTitle = '$categorysubcattitle' OR lkuTbl_SubCategory_SubCategoryTitle = '$categorysubcattitle'");
while($catsubcat_descripttitle_dishtypetitleprice = $categorysubcategory_dishtypetitleprice-&gt;fetch_assoc()) {

<i> </i>
<i> </i> $traditfavourites[$catsubcat_descripttitle_dishtypetitleprice['lkuTbl_Category_CategoryTitle']][$catsubcat_descripttitle_dishtypetitleprice['lkuTbl_SubCategory_SubCategoryTitle']][$catsubcat_descripttitle_dishtypetitleprice['lkuTbl_SubCategory_SubCategoryDescription_LrgDesc']][$catsubcat_descripttitle_dishtypetitleprice['MenuItemTitle']][] = array(
<i> </i> 'dish_type' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemType'],
<i> </i> 'dish_price' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemPrice']
<i> </i> );
<i> </i>
<i> </i> $mains_dishes[$catsubcat_descripttitle_dishtypetitleprice['MenuItemTitle']][] = array('type' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemType'], 'price' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemPrice']);
<i> </i>
<i> </i> $categorysubcategory[$catsubcat_descripttitle_dishtypetitleprice['SubCategory_SubCategoryTitle']][$catsubcat_descripttitle_dishtypetitleprice['SubCategoryDescription_LrgDesc']][$catsubcat_descripttitle_dishtypetitleprice['MenuItemTitle']][$catsubcat_descripttitle_dishtypetitleprice['MenuItemDescription']][] = array(
<i> </i> 'dish_type' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemType'],
<i> </i> 'dish_price' =&gt; $catsubcat_descripttitle_dishtypetitleprice['MenuItemPrice']
<i> </i>);
<i> </i> }
<i> </i> // while($dishtypetitleprice = $categorysubcategory_dishtypetitleprice-&gt;fetch_assoc())


It all works when Traditonal Favourites & Mains is passed into the function (Starters I've created the layout, nothing more ) but the Subcategory is where the problem is
Copy linkTweet thisAlerts:
@NogDogNov 12.2019 — All I can say is that as currently written, $categorysubcategory only exists if the while() loop executes one or more times and hits that line where you assign stuff to it. If no results are returned by the relevant query, that could be the reason it does not exist when you try to print_r() it later. You could get around the error message by initializing it at the beginning of the function with something like $categorysubcategory = []; -- but if I'm right that just means you'll now see an empty array when you get to the print_r(), so you'll still need to do some debugging as to why no query results were returned -- or add defensive code to handle it when that's a possible outcome.
Copy linkTweet thisAlerts:
@NogDogNov 12.2019 — ...Which leads to the idea of breaking up that monolithic function that's trying to do many different things (including outputting HTML :( ) into smaller, more narrowly focused functions that do just one thing (and do it well), then chain them together in the main code -- even having functions it calls call other functions. Then you can test each function separately instead of trying to figure out what's failing in a few hundred lines of stream-of-consciousness code. I don't want that to sound like a put-down, but encouragement to up your programming game and end up making life easier on yourself in the long run. :)
Copy linkTweet thisAlerts:
@rootNov 13.2019 — Simple tule of thumb, if it says Undefined variable then you bet your ass in the kentucky sweepstakes that it is infact Undefined variable and thats where you need to make a simple decision to initalize it as an empty array and then populate it rather than adhoc generation of vars within a program, it is always good practice to have at the head of your document all vars you wish to use initialised for use, preferably with control vars to test if(!$isitsetup) would test true if the variable was initialised as false and was expecting something else...

Use of isset( $yourvarname ) as a test to see if it is initialised in the program as part of program control is also a good step before using variables that have a potential to crash your program from bad data.
Copy linkTweet thisAlerts:
@nsathauthorNov 13.2019 — I've got it

it had nothing to do with the array being undeclared but more passing in the wrong value into the function

the problem was that I only looped once and passed that value where as it should have been another 2 times

correctly its stands at:
<i>
</i>foreach($categorytitles as $k =&gt; $arr) {
if($k == 'Traditional Favourites' || $k == 'Starters' || $k == 'Mains' ) { <br/>
createPageContentFromArg($k); <br/>
}
else {
foreach($arr as $sub =&gt; $subkey) {
foreach($subkey as $subval =&gt; $vals) {
foreach($vals as $titlesubkey =&gt; $titlesubval) {
if($titlesubkey == 'subcategory_title') {
createPageContentFromArg($titlesubval);
}
// if($titlesubkey == 'subcategory_title')
}
// foreach($vals as $titlesubkey =&gt; $titlesubval)
}
// foreach($subkey as $subval =&gt; $vals)
}
// foreach($arr as $sub =&gt; $subkey) <br/>
}
// else
}
// foreach($categorytitles as $k =&gt; $arr)
Copy linkTweet thisAlerts:
@nsathauthorNov 13.2019 — @NogDog#1610819

your right in terms of breaking it down into more focused functions. In terms my programming game I'm completely new to PHP. I'm learning as I go along
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...