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

Stuck on Array Key code challenge #2

I have tried 5 different way to render the code the way the editor wants it and visually it displays right for me but the challenge says I am wrong. Here is my code. What is I am missing?

<?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 . "&nbsp;" . "(" . $isbn . ")"; ?></li>
        <?php } ?>
    </ul>
</body>
</html>

3 Answers

Hi, can you post the question please?

From what I can see you may need to display it as an array (can help further if you post the question)

$books = array( "978-0743261690" => "Gilgamesh"; "978-0060931957" => "The Odyssey"; "978-0192840509" => "Aesop's Fables"; "978-0520227040" => "Mahabharta"; "978-0393320978" => "Beowulf"; );

If that doesn't work, please post the question so we can get to the bottom of this :)

Here is the question.

"Next, modify the code inside the foreach loop to display the $isbn in parentheses. After the title, add a space, an opening parenthesis, the ISBN, and a closing parenthesis."

It displays the way the question is asking...It never asked me to make it an array...

Ford,

Sorry - I assumed creating an array was the challenge.

It is asking you to make a space which in html may have been correct, but I think it requires the 'space' using concatenation. If you can see the code below the space is set out as . "" . - the two periods are for the concatenation where as the two double quotes denote a space in the HTML. What you done was perfectly fine as far as I could see but not what the challenge was after.

<?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 Five Great Books <?php foreach($books as $isbn => $book) { ?> <?php echo $book . " " . "(" . $isbn . ")"; ?> <?php } ?>

Please let me know how you get on.

Waqar

Ford,

Sorry - I assumed creating an array was the challenge.

It is asking you to make a space which in html may have been correct, but I think it requires the 'space' using concatenation. If you can see the code below the space is set out as . "" . - the two periods are for the concatenation where as the two double quotes denote a space in the HTML. What you done was perfectly fine as far as I could see but not what the challenge was after.

<?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 . " " . "(" . $isbn . ")"; ?></li> <?php } ?> </ul> </body> </html>

Please let me know how you get on.

Waqar

OK I did everything but an empty space because in the video he always says to sue "\n" as a space..

I tried that, and it still didn't work.

It might have been my trailing space before the closing <li> but I got it to work with <?php echo $books; ?> <?php echo "(" . $isbn. ")"; ?>

Great! Makes sense - the "<?php } ?>" was unnecessary.