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

Illegal string offset

I am getting two error messages in the browser with this code:

Warning: Illegal string offset 'img' in /home/treehouse/workspace/catalog.php on line 41
Warning: Illegal string offset 'title' in /home/treehouse/workspace/catalog.php on line 42

Here is my code:

<div class="section catalog page">
  <div class="wrapper">
    <h1><?php echo $pageTitle; ?></h1>

    <ul class="items">
      <?php 
        foreach($catalog as $item){
        echo "<li><a href='#'><img src='" 
          . $item["img"] . "' alt='"
          . $item["title"] . "' />"
          . "<p>View Details</p>"
          . "</a></li>";
      } 
      ?>
    </ul>

  </div>
</div>

And, for reference, here is an example of one of the items from the $catalog array:

$catalog[101] = [
    "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software",
     "img" => "img/media/design_patterns.jpg",
    "genre" => "Tech",
    "format" => "Paperback",
    "year" => 1994,
    "category" => "Books",
    "authors" => [
        "Erich Gamma",
        "Richard Helm",
        "Ralph Johnson",
        "John Vlissides"
    ],
    "publisher" => "Prentice Hall",
    "isbn" => '978-0201633610'
];

2 Answers

Hi Lindsey,

The warning that you're getting would indicate that $item is being treated as a string and not an array like the one you posted.

In an earlier part of this stage the $catalog variable was a simple array of strings.

Something like

$catalog = array("Design Patterns", "Forrest Gump", "Beethoven");

If you were to run your foreach loop on that then you would get those types of warnings. $item would be one of those strings each time through the loop.

I would make sure that you're properly including the data.php file and that you've removed the older simpler array from your code.

If you're doing this in workspaces then you can post a snapshot of your workspace if you're still having trouble resolving this.

You were totally right—I forgot to delete the old array from my catalog.php file, and it was pulling in the wrong information. I deleted it and everything works perfectly. Thank you!

Matthew Bilz
Matthew Bilz
15,679 Points

I would use single quotes for the array keys, i.e. $item['img'] since the string you're echoing is encased in double quotes. It's treating the double quote array key as part of the string itself versus the array key you wish to draw the value from.

Furthermore, it may be worth your while to change the syntax of statements like this to make it easier and not have to remember all of the jump roping between single and double quotes - but to each their own! Something like:

<?php 
    foreach($catalog as $item) : ?>
    <li>
    <a href='#'>
    <img src="<?php echo $item['img']; ?>"  alt="<?php echo $item['title']; ?>"  />
      <p>View Details</p>
      </a></li>
  <?php end foreach; ?>

Hi Matthew,

The double quotes around img are not within a double quoted string. There are several double quoted strings and values being joined together there.

Also, I don't think it was expressly pointed out in the videos, but the reason for echoing the li's as one long string was to eliminate whitespace between the li's in the source html.

The li's have display: inline-block and if there's whitespace in the html then there will be a gap between the items on the rendered page which you'd have to contend with another way.