Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Displaying Categories

Flo Tubes
Flo Tubes
5,110 Points

errormessage:Warning: Invalid argument supplied for foreach() in /home/treehouse/workspace/catalog.php on line 38

I always get this error message even tho my Code is just like the one in the Video.

... <ul class="items"> <?php $categories = array_category($catalog,$section); foreach($categories as $id){ echo get_item_html($id,$catalog[$id]); }...

2 Answers

Nathan Ward
Nathan Ward
7,907 Points

Hi,

It seems there could be a problem with the array_category() function, if you could post the code you have for this so I could help you more. You could also look back at the video to compare the function you have and the function that is created in the video.

Sue Dough
Sue Dough
35,800 Points

Check if its an array first

<?php

if ( is_array($categories) {
  // your code
} else {
  echo "Not an array";
}

This will prevent errors but may not be what you need for the quiz. You could be doing something else wrong or just have no categories? However you should almost always add fallback in the real world on arrays to avoid errors.