Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter购物车:收到一个通知,上面写着-试图在我的控制器中获取非对象的属性_Php_Codeigniter_Shopping Cart - Fatal编程技术网

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

Php Codeigniter购物车:收到一个通知,上面写着-试图在我的控制器中获取非对象的属性,php,codeigniter,shopping-cart,Php,Codeigniter,Shopping Cart,我是codeigniter的新手,在设置购物车时遇到了一些麻烦。plzz帮助 这是我的模型: class shop_model extends CI_Model { function __construct() { // Call the Model constructor parent::__construct(); } function get_a

我是codeigniter的新手,在设置购物车时遇到了一些麻烦。plzz帮助

这是我的模型:

    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');
        }
    }
我的购物车(查看)页面:


项目名称
选项
价格

在您尝试用您的店铺模型捕捉到$prod_列表后,您必须验证是否存在捕捉到的$prod_列表。。(我不太确定您的方法get返回了什么,但假检查应该足够了)


第一条建议添加检查此$prod_列表=$this->shop_model->get($this->input->post('id'))是否为空。。因为您不确定它是否存在..谢谢:)这很有帮助:)
    <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-->
    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');
    }