2017-03-03 42 views
0

所以我一直都在努力學習如何創建一個MVC,但我不能弄清楚怎麼回事錯試圖讓非對象錯誤的財產在我的PHP代碼

好像我忘了張貼錯誤: 公告:嘗試獲取第2行的C:\ xampp \ htdocs \ my_mvc \ views \ producten \ show.php中非對象的屬性

注意:試圖獲取C:\ xampp \ htdocs中非對象的屬性第3行上的\ my_mvc \ views \ producten \ show.php

注意:試圖獲取第4行的C:\ xampp \ htdocs \ my_mvc \ views \ producten \ show.php中的非對象的屬性

觀點: 的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> 

控制器: 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'); 
    } 
    } 
?> 

模型: 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']); 
    } 
    } 
?> 

如果有人能幫助我,這將是驚人的

+0

請發佈你想修復的錯誤日誌。 – thodic

+0

似乎忘記了,現在添加它。 –

回答

0

嘗試更換您的線條

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

隨着

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

()方法

+0

改變它並相應地編輯了show.php,但我得到了相同的錯誤 –

+0

你應該添加var_dump($ product)行;在你的頂部show.php文件。如果結果爲NULL,則檢查您的Product :: find($ etal)函數 –

+0

vardump returns:array(1){[0] => object(Product)#4(4){[「etal」] => string 8)「testetal」[「naam」] => string(8)「testnaam」[「prijs」] => string(3)「123」[「omsch」] => string(9)「testomsch」}} –