Author Topic: Undefined index error  (Read 423 times)

0 Members and 1 Guest are viewing this topic.

Offline gwhTopic starter

  • Enthusiast
  • Posts: 82
    • View Profile
Undefined index error
« on: March 18, 2010, 05:23:26 AM »
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&amp;itemTypeID=1\">Suits</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=10\">Vests</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=16\">Formal Shirts</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=12\">Bow Ties</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=15\">Cummerbunds</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=11\">Formal Ties</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;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.

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Undefined index error
« Reply #1 on: March 18, 2010, 05:28:15 AM »
$items is a numerically indexed array. Have a look at it through print_r() and you will see.

Offline Wolphie

  • Devotee
  • Posts: 681
  • Gender: Male
  • If you ask a question; please help answer one!
    • View Profile
Re: Undefined index error
« Reply #2 on: March 18, 2010, 05:29:30 AM »
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.
Science without religion is lame. Religion without science is blind.

Offline gwhTopic starter

  • Enthusiast
  • Posts: 82
    • View Profile
Re: Undefined index error
« Reply #3 on: March 18, 2010, 05:48:30 AM »
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&amp;itemTypeID=1\">Suits</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=10\">Vests</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=16\">Formal Shirts</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=12\">Bow Ties</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=15\">Cummerbunds</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;itemTypeID=11\">Formal Ties</a>
	
	
	
  <a href=\"
$url_consumer/index.php?subcatID=6&amp;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.

Offline gwhTopic starter

  • Enthusiast
  • Posts: 82
    • View Profile
Re: Undefined index error
« Reply #4 on: March 18, 2010, 05:53:17 AM »
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
« Last Edit: March 18, 2010, 06:02:37 AM by gwh »

Offline gwhTopic starter

  • Enthusiast
  • Posts: 82
    • View Profile
Re: Undefined index error
« Reply #5 on: March 18, 2010, 06:30:49 AM »
Worked it out guys:

$item = $items[0]['subcategory'];

SOLVED