Codeigniter 购物车:收到一条通知,上面写着 - 试图在我的控制器中获取 属性 的非对象

Codeigniter shopping cart: getting a notice that says - Trying to get property of non-object in my controller

我是 codeigniter 的新手,我在设置购物车时遇到了一些问题。请帮忙!!

这是我的模型:

    class shop_model extends CI_Model
    {
         function __construct()
         {
              // Call the Model constructor
              parent::__construct();
         }

         function get_all() {

            $results = $this->db->get('product')->result();

            return $results;

        }


         function get($id) {

            $results = $this->db->get_where('product', array('id' => $id))->row_array();
            $result = $results[0];
            //result()
            return $result;
        }

    }

我的控制器:

    class shop extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
            //$this->load->library(array('form_validation', 'email','pagination'));
            $this->load->database();
            $this->load->model('shop_model');
        }

        public function index()
        {
            $data['products'] = $this->shop_model->get_all();
            //print_r($data['products']);
            $this->load->view('template/header');
            $this->load->view('watch_stop/vprod_list', $data);
            $this->load->view('template/footer');


        }

        public function prod_cart(){
            $data['title'] = 'Shopping Cart | Watch Stop';

            $this->load->view('template/header');
            $this->load->view('watch_stop/vcart', $data);
            $this->load->view('template/footer');
        }


        function add(){
            $prod_list = $this->shop_model->get($this->input->post('id'));
            $id = $this->input->post('id');
            $insert = array(
                'id' => $this->input->post('id'),
                'qty' => 1,
                'price' => $prod_list->price,
                'name' => $prod_list->title
            );

            $this->cart->insert($insert);
            print_r($this->cart->contents());
            //redirect('shop/prod_cart');
        }
    }

我的购物车(查看)页面:

    <div class="row">
        <div class="col-md-9">
        <?php if ($cart = $this->cart->contents()): ?>
        <div id="cart">
            <div class="table-responsive">          
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>Item Name</th>
                            <th>Option</th>
                            <th>Price</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($cart as $item): ?>
                        <tr>
                            <td><?php echo heading($item['brand'].' - '.$item['gender'], 5).br().$item['title'];?></td>
                            <td><?php ?></td>
                            <td><?php echo $item['subtotal']; ?></td>
                            <td class="remove"><?php anchor('shop/remove'.$item['rowid'], '<i class="fa fa-times fa-2x"></i>'); ?>        </td>
                        <tr>
                        <?php endforeach; ?>

                        <tr class="total">
                            <td colspan="2"><strong>Total:</strong></td>
                            <td>LKR <?php echo $this->cart->total(); ?></td>
                        </tr>
                    </tbody>
                </table>
            </div> <!--table-responsive end-->
        </div> <!--cart end-->
        <?php endif; ?>
        </div> <!--col-md-9 end-->
    </div> <!--.row end-->

我的产品列表(视图)页面(此页面通过另一个控制器和模型填充并且运行良好):

    <div class="row na_top ws-brand-head">
        <!--column 1 start-->
        <div class="col-md-12">
            <?php
               echo heading($sub_head, 2);
                echo '<hr>';
            ?>
        </div> <!--column 1 end-->
    </div> <!--row end-->

    <div class="row">
        <?php echo $this->session->flashdata('msg'); ?>
        <?php foreach($products as $prod_list): ?>
        <!--Column 2 start-->
        <div class="col-md-3 ws-brand">

            <div id="products" class="naprod naprod-mar">
                <?php
                    $na_1 = array(
                                'src' => base_url().$prod_list['image'],
                                'height' => '150',
                                'alt' => $prod_list['title']
                            );

                    echo form_open('shop/add');
                    echo heading($prod_list['stock'], 5);
                    echo '<div class="na-img">'.img($na_1).'</div>';

                    echo '<div class="nacont">';
                    echo         anchor(site_url('product/'.$prod_funct.'/'.$prod_list['id']),'<p class="na-btn">VIEW</p>');
                ?>
                    <!--<input type="text" id="id" name="id" value="<?php //echo $prod_list['id'] ; ?>"  style=" display:none;"/>-->
                    <input type="hidden" name="id" value="<?php echo $prod_list['id'] ; ?>" />
                    <button type="submit" name="action" class="na-btn"><i class='fa fa-shopping-cart  fa-lg'></i></button>

                <?php
                    echo '</div>';

                    echo br();
                    echo heading('LKR '.$prod_list['price'], 5);
                    echo '<div id="'.$prod_list['brand'].'_'.$prod_list['gender'].'_'.$prod_list['id'].'">'.$prod_list['title'].'</div>';
                    echo form_close();
                ?>
            </div> <!--naprod naprod-mar end-->


        </div> <!--column 2 end-->
        <?php endforeach; ?>

    </div> <!--row end-->

当我尝试加载 shop/add 方法时出现以下错误: 1) 尝试在我的控制器商店中获取非对象的 属性:第 40 和 41 行。那是我创建 $insert 数组 'price' 和 'name'.[= 的行15=]

你必须在你尝试用你的商店模型捕获它之后验证是否捕获了 $prod_list..(我不太确定你的方法返回了什么但错误检查应该足够了)

    function add(){
        $prod_list = $this->shop_model->get($this->input->post('id'));
        if( ! $prod_list ) {
            echo "Product doesn't exists";
            exit; 
        }
        $id = $this->input->post('id');
        $insert = array(
            'id' => $this->input->post('id'),
            'qty' => 1,
            'price' => $prod_list->price,
            'name' => $prod_list->title
        );

        $this->cart->insert($insert);
        print_r($this->cart->contents());
        //redirect('shop/prod_cart');
    }