0

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

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

0

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

Guillaume Sainthillier
  • 1,655
  • 1
  • 9
  • 13
  • changed it and edited show.php accordingly but i get the same error –  Mar 03 '17 at 15:02
  • you should add the line var_dump($product); at the top of you show.php file. If result is NULL, then check your Product::find($etal) function – Guillaume Sainthillier Mar 03 '17 at 15:14
  • 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" } } –  Mar 03 '17 at 15:39
  • OK, you got an array of Product instead of a Product. Which method is called when you have you error, index() or show() method ? – Guillaume Sainthillier Mar 03 '17 at 15:45
  • index() is called when the error is given –  Mar 03 '17 at 15:51
  • Are you sure you've changed the line require_once('views/producten/show.php'); by require_once('views/producten/index.php'); in your index() method ? – Guillaume Sainthillier Mar 03 '17 at 15:53
  • xampp had trouble updating but yes its been replaced and i now get the errors Notice: Undefined variable: producten in C:\xampp\htdocs\my_mvc\views\producten\index.php on line 3 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\my_mvc\views\producten\index.php on line 3 –  Mar 03 '17 at 15:59
  • Please replace the 2 lines I mentionned in my very first response :) – Guillaume Sainthillier Mar 03 '17 at 15:59
  • changed foreach($producten as $product){} to foreach($product as $product){} but now the method show() doesnt display anything –  Mar 03 '17 at 16:00
  • Yes, you don't need to change this lines. You just need to change the line $product = Product::all(); by $producten = Product::all(); on your index() method. – Guillaume Sainthillier Mar 03 '17 at 16:03
  • oh yea ofcourse ,would make sense –  Mar 03 '17 at 16:04
  • Yes ! You cannot call the same variable name during a foreach – Guillaume Sainthillier Mar 03 '17 at 16:08
  • hmmm post() method gives object(Product)#4 (4) { ["etal"]=> NULL ["naam"]=> NULL ["prijs"]=> NULL ["omsch"]=> NULL } in var_dump() now –  Mar 03 '17 at 16:20
  • can you solve this ticket and open a new one with the code related to this new problem ? – Guillaume Sainthillier Mar 03 '17 at 16:24