Alex Pennings Alex Pennings - 2 months ago 14
PHP Question

Trying to get property of non-object error in my php code

so ive been trying to learn how to create an mvc but i just can figure out whats going wrong

seems i forgot to post the error:
Notice: Trying to get property of non-object in C:\xampp\htdocs\my_mvc\views\producten\show.php on line 2

Notice: Trying to get property of non-object in C:\xampp\htdocs\my_mvc\views\producten\show.php on line 3

Notice: Trying to get property of non-object in C:\xampp\htdocs\my_mvc\views\producten\show.php on line 4

views:
index.php:

<p>Here is a list of all producten:</p>

<?php foreach($producten as $product) { ?>
<p>
<?php echo $product->etal; ?>
<a href='?controller=producten&action=show&etal=<?php echo $product->etal; ?>'>See content</a>
</p>
<?php } ?>


show.php:

<p> requested products</p>
<p><?php echo $product->etal; ?></p>
<p><?php echo $product->naam; ?></p>
<p><?php echo $product->prijs; ?></p>


controller:
producten_controller.php:

<?php
class productenController {
public function index() {

$product = Product::all();
require_once('views/producten/show.php');
}

public function show() {

if (!isset($_GET['etal']))
return call('pages', 'error');


$product = Product::find($_GET['etal']);


require_once('views/producten/show.php');
}
}
?>


model:
product.php:

<?php
class Product {

public $etal;
public $naam;
public $prijs;
public $omsch;

public function __construct($etal, $naam, $prijs,$omsch) {
$this->etal = $etal;
$this->naam = $naam;
$this->prijs = $prijs;
$this->omsch = $omsch;
}

public static function all() {
$list = [];
$db = Db::getInstance();
$req = $db->query('SELECT * FROM producten');


foreach($req->fetchAll() as $product) {
$list[] = new product($product['etal'], $product['naam'], $product['prijs'],$product['omsch']);
}

return $list;
}

public static function find($etal) {
$db = Db::getInstance();

$etal = intval($etal);
$req = $db->prepare('SELECT * FROM producten WHERE etal = :etal');

$req->execute(array('etal' => $etal));
$product = $req->fetch();

return new product($product['etal'], $product['naam'], $product['prijs'],$product['omsch']);
}
}
?>


if anyone could help me with this that would be amazing

Answer

Try to replace your lines

 $product = Product::all();
 require_once('views/producten/show.php');

With

 $producten = Product::all();
 require_once('views/producten/index.php');

in your index() method