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 trialTomasz Bartosiewicz
Courses Plus Student 3,910 PointsUndefined index: amount in F:\xampp\htdocs\nauka\cookbook.php on line 18
I have a bug, I have looked through the code 10 times and I have no idea how to fix the code. So I ask for help. For every hint I would be very grateful. I would prefer a hint rather than a ready solution, because I want to understand the idea of programming :-)
I've got 2 Notices:
My First Recipe Notice: Undefined index: amount in F:\xampp\htdocs\nauka\cookbook.php on line 18 Egg Notice: Undefined index: amount in F:\xampp\htdocs\nauka\cookbook.php on line 18 cup Flour
And my code is:
recipe.php
<?php
class Recipe
{
private $title;
public $ingredients = array();
public $instructions = array();
public $yield;
public $tag = array();
public $source = "Alena Holligan";
private $measurements = array(
"tsp",
"tbsp",
"cup",
"oz",
"lb",
"fl oz",
"pint",
"quart",
"gallon"
);
public function setTitle($title)
{
$this->title = ucwords($title);
}
public function getTitle()
{
return $this->title;
}
public function addIngredient($item, $amount = null, $measure = null)
{
if ($amount != null && !is_float($amount) && !is_int($amount))
{
exit("The amount must be a float: " . gettype($amount) . " $amount given");
}
if ($measure != null && !in_array(strtolower($measure), $this->measurements))
{
exit("Please enter a valid measurement: " . implode(", ", $this->measurements));
}
$this->ingredients[] = array(
"item" => ucwords($item),
"amonut" => $amount,
"measure" => strtolower($measure)
);
}
public function getIngredients()
{
return $this->ingredients;
}
public function displayRecipe()
{
return $this->title . " by " . $this->source;
}
}
cookbook.php
<?php
include "classes/recipe.php";
$recipe1 = new Recipe();
$recipe1->source = "Grandma Holligan";
$recipe1->setTitle("my first recipe");
$recipe1->addIngredient("egg", 1);
$recipe1->addIngredient("flour", 2, "cup");
$recipe2 = new Recipe();
$recipe2->source = "Betty Crocker";
$recipe2->setTitle("My Second Recipe");
echo $recipe1->getTitle();
foreach ($recipe1->getIngredients() as $ing)
{
echo "\n" . $ing["amount"] . " " . $ing["measure"] . " " . $ing["item"];
}
Moderator edited for formatting: please see the Markdown Cheatsheet at the bottom of the "Add an Answer" section for instructions on how to properly post your code to the Community
2 Answers
Jennifer Nordell
Treehouse TeacherI believe you have a spelling error in this section:
$this->ingredients[] = array(
"item" => ucwords($item),
"amonut" => $amount, //this should be "amount" => $amount,
"measure" => strtolower($measure)
);
Make that correction and see if it helps!
Tomasz Bartosiewicz
Courses Plus Student 3,910 Pointsuuuuuooooooooo! a tiny piece of cake, such a big nothing, how could I missed it... ufff thanks a lot!
and sorry for not formating, but I did not see the formatting options when pasting code, next time I will look closely.