• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another Error in shopping cart

#1
[eluser]clintonbeattie[/eluser]
Hi,

I have run into another error while building a shopping cart. Apologies for these posts but I think once I get a good grounding in what these errors mean they will help in future projects.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$MOrders

Filename: controllers/welcome.php

Line Number: 67

Fatal error: Call to a member function updateCart() on a non-object in C:\xampp\htdocs\codeigniter\system\application\controllers\welcome.php on line 67

Line 67 is referring to this "$this->MOrders->updateCart($productid,$fullproduct);"

Here is my updatecart model...

Code:
class MOrders extends Model {
    function MOrders() {
        parent::Model();
    }
    
    function updateCart($productid,$fullproduct) {
    //pull in existing cart first!
    $cart = $_SESSION['cart'];//$this->session->userdata('cart');
    $totalprice = 0;
    if (count($fullproduct)){
        if (isset($cart[$productid])){
            $prevct = $cart[$productid]['count'];
            $prevname = $cart[$productid]['name'];
            $prevprice = $cart[$productid]['price'];
            
            $cart[$productid] = array(
                    'name' => $prevname,
                    'price' => $prevprice,
                    'count' => $prevct + 1
                    );
        } else {
            $cart[$productid] = array(
                    'name' => $fullproduct['name'],
                    'price' => $fullproduct['price'],
                    'count' => 1
                    );            
        }
        foreach ($cart as $id => $product){
            $totalprice += $product['price'] * $product['count'];
        }        
        
        $_SESSION['totalprice'] = ($totalprice);
        //$this->session->set_userdata('totalprice', $totalprice);
        $_SESSION['cart'] = $cart;
        //$this->session->set_userdata('cart',true);
        $this->session->set_flashdata('conf_msg', "We've added this product to your cart.");
    }
    }
    
}

and this is my updatecart function in my 'welcome' controller...

Code:
function cart($productid=0){
    if ($productid > 0){
        //$productid = $this->uri->segment(3);
        $fullproduct = $this->MProducts->getProduct($productid);
        $this->MOrders->updateCart($productid,$fullproduct);
        redirect('welcome/product/'.$productid, 'refresh');    
    } else {
        $data['title'] = "Claudia's Kids | Shopping Cart";
        if (count($_SESSION['cart'])){
            $data['main'] = 'shoppingcart';
            $data['navlist'] = $this->MCats->getCategoriesNav();
            $this->load->vars($data);
            $this->load->view('template');    
        } else {
            redirect('welcome/index','refresh');
        }
    }
      }

The session library has been autoloaded and the session_start() implemented. Can anyone shed any light on this?

Thanks again. You've been great so far.

C

#2
[eluser]Nathan Payne[/eluser]
You are using the codeigniter book aren't you?

I used that for a while and realised that some of the code didn't work so reverted to the forums and guides o the net.

You are having one of the problems I had, so I tried to build my own shopping cart lol.

I will keep an eye on this thread though, see if someone directs you to the solution.

#3
[eluser]Unknown[/eluser]
I believe it means that $this->MOrders is null, hence you are trying to call a function on a non-object. It's probably not loading correctly. Can you list two things?

1) The line that loads the model in the controller
2) The name of the file that contains the model

I have run into this before when the filename containing the model started with an upper case character.

-pc

#4
[eluser]leozzz[/eluser]
[quote author="puppycrack" date="1231744783"]I believe it means that $this->MOrders is null, hence you are trying to call a function on a non-object. It's probably not loading correctly. Can you list two things?

1) The line that loads the model in the controller
2) The name of the file that contains the model

I have run into this before when the filename containing the model started with an upper case character.

-pc[/quote]


I found the error, you have to add the new model to autoload.php
where the book forgot to mention this point.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.