gwh Posted March 18, 2010 Share Posted March 18, 2010 Hi everyone, The following code in a controller file shows that I have an array that's been built after querying the database (I haven't shown the select query so the post won't be too long): $items = array(); while ($row = mysqli_fetch_array($result)) { $items[] = array('itemTitle' => $row['itemTitle'], 'itemSKULadies' => $row['itemSKULadies'], 'itemSKUMen' => $row['itemSKUMen'], 'itemDescLadies' => $row['itemDescLadies'], 'itemDescMen' => $row['itemDescMen'], 'itemPriceBoth' => $row['itemPriceBoth'], 'itemPriceFemale' => $row['itemPriceFemale'], 'itemPriceMale' => $row['itemPriceMale'], 'itemColoursBoth' => $row['itemColoursBoth'], 'itemColoursFemale' => $row['itemColoursFemale'], 'itemColoursMale' => $row['itemColoursMale'], 'Lsize' => $row['Lsize'], 'Msize' => $row['Msize'], 'itemType' => $row['itemType'], 'itemSwatchBoth' => $row['itemSwatchBoth'], 'itemSwatchFemale' => $row['itemSwatchFemale'], 'itemSwatchMale' => $row['itemSwatchMale'], 'itemImage' => $row['itemImage'], 'subcategory' => $row['subcategory']); } include 'catalogue_consumer.php'; exit(); } ?> You can see above that it includes a template file catalogue_consumer.php. Part of this template file contains the code shown below. <?php if ($items['subcategory'] == Formalwear) { echo "<a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=1\">Suits</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=10\">Vests</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=16\">Formal Shirts</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=12\">Bow Ties</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=15\">Cummerbunds</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=11\">Formal Ties</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=14\"> Cravats </a>"; } ?> You can see that I'm referencing the 'subcategory' from the $items array above: if ($items['subcategory'] == Formalwear) When I test I get an Undefined index error: Notice: Undefined index: subcategory in /Applications/MAMP/htdocs/site/consumer/catalogue/catalogue_consumer.php on line 151 This errors refers to the line above, ie if ($items['subcategory'] == Formalwear) I don't understand why it's saying it's undefined since it's in the array mentioned above. So I just wondered how I can define it so the error will be eliminated? Appreciate any assistance. Link to comment https://forums.phpfreaks.com/topic/195657-undefined-index-error/ Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 $items is a numerically indexed array. Have a look at it through print_r and you will see. Link to comment https://forums.phpfreaks.com/topic/195657-undefined-index-error/#findComment-1027998 Share on other sites More sharing options...
Wolphie Posted March 18, 2010 Share Posted March 18, 2010 That's because $items['subcategory'] doesn't exist. $items is already an array, and you're making another array inside each element of the $items array to store your data. e.g. $items[0]['subcategory'] would return the data for the first row in the database. $items[1]['subcategory'] would return the second and so on. Link to comment https://forums.phpfreaks.com/topic/195657-undefined-index-error/#findComment-1027999 Share on other sites More sharing options...
gwh Posted March 18, 2010 Author Share Posted March 18, 2010 Thanks for the replies, I understand a bit more now. I revised the code as follows: <?php foreach ($items as $item): if ($item['subcategory'] == "Formalwear") { echo "<a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=1\">Suits</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=10\">Vests</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=16\">Formal Shirts</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=12\">Bow Ties</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=15\">Cummerbunds</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=11\">Formal Ties</a> <a href=\"$url_consumer/index.php?subcatID=6&itemTypeID=14\"> Cravats </a>"; }endforeach; ?> This has eliminated the error but it's produced another problem. For example in the Suits category there are 5 items, so it's outputting 5 lots of the 7 links above. All I want to do is output 7 links - not 1 set of 7 for each item in that category as it's doing. I think this has to do with the foreach loop, but I don't know an alternative. Any further advice appreciated. Link to comment https://forums.phpfreaks.com/topic/195657-undefined-index-error/#findComment-1028003 Share on other sites More sharing options...
gwh Posted March 18, 2010 Author Share Posted March 18, 2010 Instead of the foreach loop I tried: $item = $items[19]; But that didn't work. Actually I don't even know if that makes sense. It gave me the following error: Notice: Undefined offset: 19 in /Applications/MAMP/htdocs/site/consumer/catalogue/catalogue_consumer.php on line 149 Link to comment https://forums.phpfreaks.com/topic/195657-undefined-index-error/#findComment-1028005 Share on other sites More sharing options...
gwh Posted March 18, 2010 Author Share Posted March 18, 2010 Worked it out guys: $item = $items[0]['subcategory']; SOLVED Link to comment https://forums.phpfreaks.com/topic/195657-undefined-index-error/#findComment-1028021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.