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 Object-Oriented PHP Basics Building the Recipe Static Methods

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

How to render the static display method? [SOLVED]

Okay, so I'm trying to display my static method to the screen. I know I'm not using the console like Alena in Workspaces but I really don't see why the method isn't working.

What I'm actually getting is all the echos from the cookbook.php file but Alena left those in and we only see the output from the method.

http://www.jonniegrieve.co.uk/jg-lab.co.uk/php-cookbook/cookbook.php

Note: I'm aware I'm pulling in the same HTML twice with the include

  • I've included the Render and the Recipe class.

  • I've put the displayRecipe() method in the Recipe class.

Here's my php files as i have them at the moment,

cookbook.php
<!doctype html>
<html>
<head>
    <title>PHP: Cookbook using Object Oriented Programming</title>

    <link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
    <link href="styles.css" rel="stylesheet" type="text/css" />

</head>
<body>


    <style>
    /* Styles */

    </style>


    <header>
        <img class="logo" src = "logo.png" />
    </header>

    <div class="pp_head">
        <h1>PHP: Cookbook using Object Oriented Programming</h1>

        <p class="center"><a href="">Recipe List</a></p>
    </div>

    <div class = "content">
        <!-- -->

        <h1>Cookbook</h1>

        <?php

            include "recipe.php";
            include "render.php";

            /*to be addded to cookbook.php file.*/

            //instantiate class create a new new instance of class.
            $recipe1 = new Recipe();
            //$recipe1->setSource("Marge Simpson");
            //$recipe1->setTitle("Pretzel");
            $recipe1->addIngredients("egg", 2.3, "oz");
            //instantiate class  create a new new instance of class.
            $recipe2 = new Recipe();
            //$recipe2->setSource("Buffy Summers");
            //$recipe2->setTitle("Cross Buns");


            //Add ingredients for Recipies
            foreach($recipe1->getIngredients() as $ing) {
                    echo "\n" . $ing["amount"] . " " . $ing["measure"] . " ". $ing["item"];

                }   

            //Add instructions for Recipies
            $recipe1->addInstruction("first instruction");
            $recipe1->addInstruction("second instruction");


            echo implode("\n", $recipe1->getInstruction() );

            //Add titles for recipies
            $recipe1->setTitle("Pretzel");
            echo $recipe1->getTitle();


            //Add tags for recipies
            $recipe1->addTag("Breakfast");
            $recipe1->addTag("Main Course");

            echo implode("\n", $recipe1->getTags() );

            //Add Yield for Recipies
            $recipe1->setYield("Yield 1");

            echo $recipe1->getYield(); //implode?]
            //echo implode("\n", $recipe1->getYield() );


            //Add Source for Recipies

            $recipe1->getSource("Marge Simpson");

            echo $recipe1->getSource();


            //Display Recipe
            echo Render::displayRecipe($recipe1);
        ?>

        <!-- /*First Recipe*/ 
        <div class ="recipe">

            <h3><?php echo $recipe1->displayRecipe(); ?> </h3>      
            <p><?php echo $recipe1->getTitle(); ?> </p>


        </div>


        <div class ="recipe">
            <h3><?php echo $recipe2->displayRecipe(); ?> </h3>
            <p><?php echo $recipe2->getTitle();?> </p>

        </div>
        -->
    </div>

    <footer>
        <p>PHP: Cookbook using Object Oriented Programming. From Teehouse by Jonnie Grieve Digital Media</a></p> 
    </footer>
</body>

</html>
recipes.php
<!doctype html>
<html>
<head>
    <title>PHP: Cookbook using Object Oriented Programming</title>

    <link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
    <link href="styles.css" rel="stylesheet" type="text/css" />

</head>
<body>


    <header>
        <img class="logo" src = "logo.png" />
    </header>

    <div class="pp_head">
        <h1>PHP: Cookbook using Object Oriented Programming</h1>

        <p class="center"><a href="">Recipe List</a></p>
    </div>

    <div class = "content">
        <!-- -->

        <h1>Cookbook</h1>

        <?php

        class Recipe {

            //These are properties of the Recipe class.  Some are arrays, some are strings.     
            private $title;
            private $ingredients = array();
            private $instructions = array();
            private $recYield;
            private $tag = array();
            private $source = "Jonathan";
            private $measurements = array(
                "tsp",
                "tbsp",
                "cup",
                "oz",
                "lb",
                "fl oz",
                "pint",
                "quart",
                "gallon",

            );

            /*getters and setters for private properties*/
            public function setTitle($title) {
                  $this->title = ucwords($title);
            }


            public function getTitle() {
                  return $this->title;
            } 



            //"add" function for adding ingredients to cookbook.
            public function addIngredients($item, $amount, $measure) {


                // If there's a value of amount and it is neither a float or an integer, display a message. 
                if($amount != null && !is_float($amount) && !is_int($amount)) {
                    exit("The amount must be a float: " . gettype($amount) . " $amount given");
                }
                // if there  is a value but not a valid measurement from measurement array display a message.
                if($measure != null && !in_array(strtolower($measure), $this->measurements)) {
                    exit("<p>Please enter a valid measurement: " . implode(", ", $this->measurements) . "</p>");
                }


                //add ingredients as an associative array   "items" =>ucwords($item),
                $this->‌ingredients[] = array(
                    "items" => ucwords($item),
                    "amount" => $amount,
                    "measure" => $measure
                );
            }

            public function getIngredients() {
                return $this->ingredients;
            }

            public function addTag($tag) {
                $this->tags[] = strtolower($tag);
            }

            public function getTags() {
                return $this->tags;
            }       

            public function addInstruction($string) {
                $this->instructions[] = $string;

            }

            public function getInstruction() {
                return $this->instructions;

            }

            public function setSource($source) {
                $this->source = ucwords($source);
            }

            public function getSource() {
                return $this->source;
            }

            public function setYield($yield) {
                $this->recYield = $yield;
            }

            public function getYield() {
                return $this->recYield;
            }           

        }
        ?>



    </div>

    <footer>
        <p>PHP: Cookbook using Object Oriented Programming. From Teehouse by Jonnie Grieve Digital Media</a></p> 
    </footer>
</body>

</html>
render.php
<?php
class Render {

            /*display Recipe method*/           
            public static function displayRecipe($recipe) {

                $output = "";
                $output .= $recipe->getTitle() . " by " . $recipe->getSource();
                $output .= "\n";
                $output .= implode(", ", $recipe->getTags());


                //Since Ingredients have their own variables in their array, we need to use a foreach loop to retieve them.
                foreach($recipe->getIngredients() as $ing) {
                  $output .= $ing["amount"] . " " . $ing["measure"] . "  " . $ing["item"];
                  $output .= "\n";
                }

                return $output;

            }
}
?>
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Well it looks like I've worked it out.

I'm fairly certain having slept on it I was still only getting the echoes that were appearing on the cookbook.php file and the displayRecipe() method wasn't displaying. I may have been wrong but it doesn't matter now. :) After a bit more tinkering I was able to format the method and displaying it, switching new line escape characters for the <br /> tag since I'm outputting to HTML.

<?php

class Render {

            /*display Recipe method*/           
            public static function displayRecipe($recipe) {

                $output = "";  
                $output .= "<br /> ";   
                $output .= "Recipies";
                $output .= "<br />---------<br />";
                $output .= $recipe->getTitle() . " by " . $recipe->getSource();
                $output .= "<br />";
                $output .= implode(", ", $recipe->getTags());
                $output .= "<br />";            

                //Since Ingredients have their own variables in their array, we need to use a foreach loop to retieve them.
                foreach($recipe->getIngredients() as $ing) {
                  $output .= $ing["amount"] . " " . $ing["measure"] . "  " . $ing["item"];
                  $output .= "<br />";
                }
                /*<br /> for new line when outputting to a html file.*/
                return $output;

            }
}

?>