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 trialBen Farr
14,855 PointsFormatting is wrong and I don't know why.
This is my code for task 2 of 2 but it is saying it's wrong and that the formatting is wrong. I am not sure why because the formatting looks correct to me. Any help would be much appreciated. Thanks!
'''php <?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> '''
2 Answers
August Whitlock
1,632 PointsI coded this code into one of my projects and it ran fine. One thing that you might want to do is avoid echoing out code that you want to display to the browser. It is actually discouraged by the PHP documantation, especially when dealing with large blocks of text. You can read about it here:
http://www.php.net/manual/en/language.basic-syntax.phpmode.php
Just drop down to the Example one section on this page.
It would actually improve the page a bit, and maybe cause less issues with errors if you wrote out your code like this:
<p>PHP: Five Great Books Five Great Books</p> <?php
$books["978-0743261690"] = "Gilgamesh"; $books["978-0060931957"] = "The Odyssey"; $books["978-0192840509"] = "Aesop's Fables"; $books["978-0520227040"] = "Mahabharta"; $books["978-0393320978"] = "Beowulf"; ?> <?php foreach($books as $isbn => $book) { echo "<p> . " . $book . " (" . $isbn . ") </p>"; ?>
This seems less confusion to me, and I only echo out the paragraph tags that will display the book title and ISBN number in the foreach loop. Otherwise I leave it as plan html.
Sorry I couldn't help you, though. This code actually works in a PHP page. If the quizes are giving issues, then that might be something that you might want Treehouse to know about.
J.L. Sparreboom
3,241 PointsThis is actually valid php code and works fine as stated above. I recommend you adjust how the code is displayed. I added a break tag to make it more friendly to the eye when displaying. Also if you provide a link to the task we can take a look at it. You might actually missed something crucial for example: the foreach loop has to be at line 12 etc. I recommend you re-read your task description.
<?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
<?php
foreach($books as $isbn => $book) { ?>
<?php echo $book ." (" . $isbn. ") "; ?> <br />
<?php } ?>