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

lyonel scapino
lyonel scapino
14,191 Points

Weird error message on the browser

I have this message :

notice: array to string conversion in c:\xampp\htdocs\shirts.php on line 21 (displaying on Chrome)

While my code on shirts.php did not move:

<?php foreach ($products as $product => $product_id) { echo "<li>"; echo '<a href="shirt.php?id=' . $product_id . '">' ; echo '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">'; echo "<p>View Details</p>"; echo "</a>"; echo "</li>"; } ?>

(note that line 21 is the href line)

How come?

4 Answers

patrick kellogg
patrick kellogg
7,579 Points

try using curly braces to surround all your associative array elements like this:

{$product["img"]}

{$product["name"]}

but carlos' advice is dead on

lyonel scapino
lyonel scapino
14,191 Points

gives me an unexpected curly braces message. thks anyway.

Carlos Morales
Carlos Morales
13,880 Points

I find it easier to break in and out of php and html. For example:

<?php 
foreach ($products as $product => $product_id) 
    { 
        ?>
            <div>
            <?php echo "anything you want between the neatly formatted html" ?>
            </div>
        <?php
    }
?>

This way, debugging your program will be much easier than using concatenation. I hope this helps.

lyonel scapino
lyonel scapino
14,191 Points

I admit concatenation is not easy to read. But at some point if I want to dynamically generate some HTML I have to mingle it with PHP right?

Carlos Morales
Carlos Morales
13,880 Points

Yup! Here is a link to a video on the subject: http://teamtreehouse.com/library/build-a-simple-php-application/listing-inventory-items/mingling-php-and-html

I think it's the best way to solve the issue you're having.