Jump to content

Undefined index error


gwh

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.