We help IT Professionals succeed at work.

Error in method

Can you please help me in that error.
screenshot2.JPGscreenshot.JPG
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
class CoffeeEntity
{
    public $id;
    public $name;
    public $type;
    public $price;
    public $roast;
    public $country;
    public $image;
    public $review;
    
    function __construct($id, $name, $type, $price, $roast, $country, $image, $review) {
        $this->id = $id;
        $this->name = $name;
        $this->type = $type;
        $this->price = $price;
        $this->roast = $roast;
        $this->country = $country;
        $this->image = $image;
        $this->review = $review;
    }

}


?>

Open in new window

Comment
Watch Question

Ryan ChongSoftware Team Lead

Commented:
Have you declaring and using "CoffeeEntity" somewhere else?
pls share your codes in coffee.php for troubleshooting.

Author

Commented:
<?php

require 'Controller/CoffeeController.php';
$coffeeController = new CoffeeController();

if(isset($_POST['types']))
{
    //Fill page with coffees of the selected type
    $coffeeTables = $coffeeController->CreateCoffeeTables($_POST['types']);
}
else 
{
    //Page is loaded for the first time, no type selected -> Fetch all types
    $coffeeTables = $coffeeController->CreateCoffeeTables('%');
}

//Output page data
$title = 'Coffee overview';
$content = $coffeeController->CreateCoffeeDropdownList(). $coffeeTables;

include 'Template.php';
?>

Open in new window

Ryan ChongSoftware Team Lead

Commented:
see if you have declared "CoffeeEntity" in Controller/CoffeeController.php ?

Author

Commented:
<?php

require ("Model/CoffeeModel.php");

//Contains non-database related function for the Coffee page
class CoffeeController {

    function CreateCoffeeDropdownList() {
        $coffeeModel = new CoffeeModel();
        $result = "<form action = '' method = 'post' width = '200px'>
                    Please select a type: 
                    <select name = 'types' >
                        <option value = '%' >All</option>
                        " . $this->CreateOptionValues($coffeeModel->GetCoffeeTypes()) .
                "</select>
                     <input type = 'submit' value = 'Search' />
                    </form>";

        return $result;
    }

    function CreateOptionValues(array $valueArray) {
        $result = "";

        foreach ($valueArray as $value) {
            $result = $result . "<option value='$value'>$value</option>";
        }

        return $result;
    }
    
    function CreateCoffeeTables($types)
    {
        $coffeeModel = new CoffeeModel();
        $coffeeArray = $coffeeModel->GetCoffeeByType($types);
        $result = "";
        
        //Generate a coffeeTable for each coffeeEntity in array
        foreach ($coffeeArray as $key => $coffee) 
        {
            $result = $result .
                    "<table class = 'coffeeTable'>
                        <tr>
                            <th rowspan='6' width = '150px' ><img runat = 'server' src = '$coffee->image' /></th>
                            <th width = '75px' >Name: </th>
                            <td>$coffee->name</td>
                        </tr>
                        
                        <tr>
                            <th>Type: </th>
                            <td>$coffee->type</td>
                        </tr>
                        
                        <tr>
                            <th>Price: </th>
                            <td>$coffee->price</td>
                        </tr>
                        
                        <tr>
                            <th>Roast: </th>
                            <td>$coffee->roast</td>
                        </tr>
                        
                        <tr>
                            <th>Origin: </th>
                            <td>$coffee->country</td>
                        </tr>
                        
                        <tr>
                            <td colspan='2' >$coffee->review</td>
                        </tr>                      
                     </table>";
        }        
        return $result;
        
    }

}

?>

Open in new window

Ryan ChongSoftware Team Lead

Commented:

see if you have declared "CoffeeEntity" in Controller/CoffeeController.php ?                                  


see if you found that in Model/CoffeeModel.php?

Most Valuable Expert 2018
Distinguished Expert 2019
Commented:

If I were you, I would change all the require '...' statements to require_once '...'


It sounds like you're probably loading a file more than once which is trying to redeclare your class:


require_once 'Controller/CoffeeController.php'; 
require_once 'Model/CoffeeModel.php';

Moving forward, it will make you life a little easier if your adopt an autoloading function, This way you don't need to run the require statements - your models and controllers etc will be loaded automatically for you.

Author

Commented:
Thank You.