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 Creating the Display Function

Getting a "Warning: Illegal string offset 'img'..." message in place of every third catalog item

The full message says "Warning: Illegal string offset 'img' in /home/treehouse/workspace/inc/functions.php on line 5

Warning: Illegal string offset 'title' in /home/treehouse/workspace/inc/functions.php on line 6"

Here is the code I have:

functions: --- php

<?php function get_item_html($id,$item) {

$output = "<li><a href = '#'><img src='" 
  . $item["img"] . "'alt='" 
  . $item["title"] . "' />"
  . "<p>View Details</p>"
  . "</a></li>";

return $output; }---

And for the catalog.php file: --- php

<?php foreach ($catalog as $id => $item) { echo get_item_html ($id,$item);

}?>

And for the index.php file: --- php

<?php <ul class="items"> <?php foreach ($catalog as $id => $item) { echo get_item_html ($id, $item);

}?>

1 Answer

Maximillian Fox
PLUS
Maximillian Fox
Courses Plus Student 9,236 Points

Hey there,

I've touched up the code in here but I don't necessarily see anything wrong with your syntax. One thing I did notice however was that you needed to add a space between your start quote and alt, so they don't get mashed together when you concatenate everything.

<?php
function get_item_html( $id, $item ) {
  $output = "<li><a href = '#'><img src='" 
  . $item["img"] . "' alt='" 
  . $item["title"] . "' />"
  . "<p>View Details</p>"
  . "</a></li>";
  return $output; 
}

I make a mockup catalog array in a PHP testing environment but I couldn't replicate your issue. Could you paste the entire code of your documents (including the array for the catalog) or link to your workspace, and I may be able to find it.