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 Build a Simple PHP Application Integrating with PayPal Array Keys

Alec Jones
Alec Jones
5,510 Points

What is Wrong???

What is wrong with my code.

<?php

$books["978-0743261690"] = "Gilgamesh"; $books["978-0060931957"] = "The Odyssey"; $books["978-0192840509"] = "Aesop's Fables"; $books["978-0520227040"] = "Mahabharta"; $books["978-0393320978"] = "Beowulf";

?><html> <head> <title>Five Great Books ($isbn)</title> </head> <body> <h1>Five Great Books</h1> <ul> <?php foreach($books as $isbn => $book) { ?> <li><?php echo $books[$isbn] ?></li> <?php } ?> </ul> </body> </html>

5 Answers

Mike Baxter
Mike Baxter
4,442 Points

Just so we can read it easier, his code is the following:

<?php

$books["978-0743261690"] = "Gilgamesh";
$books["978-0060931957"] = "The Odyssey";
$books["978-0192840509"] = "Aesop's Fables";
$books["978-0520227040"] = "Mahabharta";
$books["978-0393320978"] = "Beowulf";

?>

Five Great Books ($isbn) Five Great Books

<?php
    foreach($books as $isbn => $book) {
?>

<?php echo $books[$isbn] ?>

<?php
    }
?>
Mike Baxter
Mike Baxter
4,442 Points

It looks like you might be forgetting the HTML elements. Here's the correct answer, but I HIGHLY recommend you understand what it's doing before you move on. I'm not sure why you're trying to reference $books[$isbn]. The question is trying to get you to display the ISBN, but what you're doing is using the ISBN to display the title. All you need to do is echo out both the $isbn and $book variables separately (in their proper HTML elements).

Cheers!

<?php

$books["978-0743261690"] = "Gilgamesh";
$books["978-0060931957"] = "The Odyssey";
$books["978-0192840509"] = "Aesop's Fables";
$books["978-0520227040"] = "Mahabharta";
$books["978-0393320978"] = "Beowulf";

?><html>
<head>
    <title>Five Great Books</title>
</head>
<body>
    <h1>Five Great Books</h1>
    <ul>
        <?php foreach($books as $isbn=>$book) { ?>
            <li><?php echo $book; ?> (<?php echo $isbn; ?>)</li>
        <?php } ?>
    </ul>
</body>
</html>

Hi Alec,

Try this:

 <?php foreach($books as $isbn => $book) { ?>
            <li><?php echo $book . " (" . $isbn . ")"; ?></li>
        <?php } ?>

I also agree with Mike that you need to make sure you understand each section before you move on. I would suggest watching the video and giving it another shot. Good luck!

:-)

Brian Molkovich
Brian Molkovich
11,333 Points

Thought but did not say) Try to "echo" something) You think or tell me what you missed?