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 trialLindsey Whitney
12,604 PointsIllegal 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
Jason Anello
Courses Plus Student 94,610 PointsHi 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.
Matthew Bilz
15,829 PointsI 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; ?>
Jason Anello
Courses Plus Student 94,610 PointsHi 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.
Lindsey Whitney
12,604 PointsLindsey Whitney
12,604 PointsYou 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!