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

Alexandr Arpentin
Alexandr Arpentin
958 Points

can someone help me with my code. i'm having an error- Warning: Illegal string offset

this is a part of my code:

  <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>
Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Can you provide also your array declaration?

3 Answers

Simon Coates
Simon Coates
28,694 Points

that code looks fine. what leads you to think that snippet is the problem? Did you try removing it temporarily and see if the error goes away. I ran

<?php
$catalog = [
    0=> [        "img"=>"imgval",        "title"=>"thetitle"              ]
    ];
?>
<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>

and got

<ul class ="items"> 
<li><a href='#'><img src='imgval' alt='thetitle' /><p>View Details</p></a></li>
</ul>

seems okay.

Alexandr Arpentin
Alexandr Arpentin
958 Points

sorry for bad englishknowledge. let me try to explain, i'm just trying to follow Displaying All Items video in Building basic PHP website section and that's what i'm getting: Warning: Illegal string offset 'img' in /home/treehouse/workspace/catalog.php on line 31 Warning: Illegal string offset 'title' in /home/treehouse/workspace/catalog.php on line 32

and looks like this topic - https://teamtreehouse.com/community/illegal-string-offset is not helping me.

yes , i'm removed some parts of the code, to see whats happening, but this lesson is a little bit tricky for me, and this manipulation not making any sense.

Simon Coates
Simon Coates
28,694 Points

took a look at https://teamtreehouse.com/community/illegal-string-offset . Her problem was the catalog declaration - her code was effectively similar to the following demo.

<?php
$catalog = array("Design Patterns", "Forrest Gump", "Beethoven"); //problem was here.
?>
<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>

The problem is that $item is a string, so $item['img'] is illegal. So you need to take a look at what is in $catalog (can use var_dump).

Alexandr Arpentin
Alexandr Arpentin
958 Points

Sorry can you discribe in more detail, that step with var dumping $catalog.

Anyway thank you very much! I will try tommorow, spend a half of night with this)

Simon Coates
Simon Coates
28,694 Points
<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>

The above code works fine, so long as catalog is set to an array of arrays and not an array of strings. so you need to see what is currently stored in $catalog. You can do this using

var_dump($catalog);