PHP Build a Basic PHP Website 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
PRO
Maximillian Fox
Pro Student 9,233 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.