I'm getting the following fatal error in PHP:
Fatal error: Call to undefined method CoffeeModel::GetCoffeeByType() in C:\wamp\www\12.01.2015 Class 01\Coffee Website\Controller\CoffeeController.php on line 37
Call Stack
# Time Memory Function Location
1 0.0030 142264 {main}( ) ..\Coffee.php:0
2 0.0130 162800 CoffeeController->CreateCoffeeTables( ) ..\Coffee.php:15
The code for the above error is shown below:
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;
}
In this Code the CoffeeModel Class are present
<?php
require ("Entities/CoffeeEntity.php");
// contains database related code for the coffee type
class CoffeeModel
{
// Get all coffee types from the database and return them in an array
function GetCoffeeTypes()
{
require 'Credentials.php';
//Open connection and Select database
mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($database);
$result = mysql_query("SELECT DISTINCT type FROM coffee") or die(mysql_error());
$types = array();
// Get data from databse
while($row = mysql_fetch_array($result))
{
array_push($types, $row[0]);
}
// Close connection and return
mysql_close();
return $types;
}
}
// GET coffeeEntity objects from the database and return them in an array.
function GetCoffeeByType($type)
{
require 'Credentials.php';
// Open connection and select database
mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($database);
$query = "SELECT * FROM coffee WHERE LIKE '$type'";
$result = mysql_query($query) or die (mysql_error());
$coffeeArray = array();
//GET Data from Database
while ($row = mysql_fetch_array($result))
{
$name = $row[1];
$type = $row[2];
$price = $row[3];
$roast = $row[4];
$country = $row[5];
$image = $row[6];
$review = $row[7];
// Create Coffee objects and store them in an array
$coffee = new CoffeeEntity (-1, $name, $type, $price, $roast, $country, $image, $review);
array_push($coffeeArray, $coffee);
}
// CLose connection and return result
mysql_close();
return $coffeeArray;
}
How can I fix this?
Ok, focusing on only the issue posted, your problem is that you close the class after the first method:
}
} <---- this closing bracket is terminating the class
// GET coffeeEntity objects from the database and return them in an array.
function GetCoffeeByType($type)
{
Just move that one bracket to the end of the file and it should work. Next after that is to brush up on class organization but that's another story.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments