Call to undefined method; how to fix?

Sheikh Noman Mehmood

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?

Kai Qing

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix Uncaught TypeError: Cannot call method 'call' of undefined in fullcalendar

How to fix 'Uncaught Error: Call to undefined method' in PHP

How to fix Call to undefined method Maatwebsite\Excel\Excel::create()?

How to Fix "Attempted to call an undefined method named "createNamedBuilder..."?

How to fix 'Call to undefined method Illuminate/Database/Eloquent/Relations/BelongsTo::attach()'

How to fix "undefined method" error in destroy function

How to fix "undefined method `empty?' for ...:Float"

How to fix the error "Cannot read property 'call' of undefined. Exception occurred when checking element , check the 'rules' method"?

How to fix 'undefined method error' when method exists?

How to fix Call to undefined function get_woocommerce_currency() in wordpress?

How to fix 'The method mismatch(int[], int[]) is undefined for the type Arrays'

How to fix undefined method `new' for "Rack::Cors":String

How to fix "NoMethodError (undefined method []' for nil:NilClass): app/controllers/..."

How to fix 'undefined method' enum in rspec test with FactoryBot?

How to fix undefined method in nested models? post/create in Rails app

Properties inside method showing undefined but working outside the methods . How to fix it?

Call to undefined method error

Call to undefined method links()

"Call to undefined method ..."

Call to undefined method onlyTrashed()

Can not call method of undefined

Best way to fix undefined method

How to fix submit form call http method get with query params

How to fix this Swift compiler error for onReceive method call?

How to fix 'attempt to call method 'addMoney' (a nil value)' error?

How to fix "Undefined ReadFile"

How to fix undefined array

How to fix "this is undefined" in VueJS?

how to Fix undefined variable?