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

General Discussion

Code Challenge Array Keys Stage 2

Hi I am having trouble with the code challenge: array keys stage 2, not sure where am i going wrong. can anyone help :) please find the code posted below:

<?php

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

foreach($books as $isbn=> $book);{ echo $isbn; }

?><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 } ?> <?php } ?> </ul> </body> </html>

10 Answers

Hey Chris,

The parentheses in your code are part of the html code and not in the php part of the code. Try concatenating it all into one string.

<?php echo $book . ' (' . $isbn . ')';

Hope that helps! :)

I dont get this at all it says:

"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 says above "modify the code inside the foreach loop" there are 2 foreach loops on the sheet (which one do u modify?)

Then it goes on to say "After the title add a space, an opening parenthesis, the ISBN, and a closing parenthesis." so am guessing that is:

<title>Five Great Books</title>  (' . $isbn . ')

Can someone help please?

Hey Chris,

This is the beginning code from the task 2 code challenge. There should only be one foreach loop.

<?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; ?></li>
        <?php } ?>
    </ul>
</body>
</html>

The title for the book is coming from inside the foreach loop.. >>>> echo $book <<<< You were on the right track from your original post, but the formatting of your code was off. The parenthesis had to be all inside the php block, but you were putting them outside of the php blocks.

<?php echo $book; ?> 
(
<?php echo $isbn; ?>
)
<?php } ?>
<?php } ?>

The instructions for this task are:

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.

Broken down in little steps, the task looks like this:

After the title: echo $book;
add a space: echo $book . ' ';
an opening parenthesis: echo $book . ' ' . '(';
the ISBN: echo $book . ' ' . '(' . $isbn;
and a closing parenthesis: echo $book . ' ' . '(' . $isbn . ')';

Using concatenation (the period), you can break the instructions down one by one to complete the the task and "glue" the different pieces of the task together to output the complete string.

In hopes of not confusing you, you can even simplify this a little by marrying the space and open parenthesis:

<?php echo $book . ' (' . $isbn . ')'; ?>

I hope this helps. :)

This answer is a great explanation! It works!

Thanks for going into so much detail for me mike :) your a legend.

haha yes i did it mike woooo :) dead happy now, think the main problem was that i added two foreach loops into the php, what an idiot hehe :)

Thanks again mike :)

Chris

im having the exact same issue.... but im 99.9% sure my code is correct here is my foreach loop

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

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

This is my loop. When I view it in the browser it looks right to me..what am I doing wrong??!

Should look like this... <?php echo $book . ' ' . '(' . $isbn . ')'; ?> . Semantics is important, regardless if it looks right or not. Good luck.

Somebody didn't do a very good job explaining this one.

So this is the code...

<li><?php echo $books . ' (' . $isbn . ')'; ?></li>

The reason why all the above posts do not work is because everyone posted $book when it should be $books.