代码之家  ›  专栏  ›  技术社区  ›  Recency

无法在我的CI\U控制器的写入上下文中使用函数返回值

php
  •  1
  • Recency  · 技术社区  · 8 年前
        <?php
        class Cart extends CI_Controller{
    
       public $paypal_data = '';
       public $tax;
       public $shipping;
       public $total =0;
       public $grand_total;
    
       //cart index
       public function index(){
        //load view
        $data('main_content') = 'cart';
        $this->load->view('layouts/main', $data);
       }//end function
    
       //add to Cart
       public function add(){
        //item data in a array construct
        $data = array('id'=> $this->input->post('item_number'), 'qty'=>   $this->input->post('qty'), 'price'=> $this->input->post('price'), 'name'=>  $this->input->post('title'));//end statement
    
       }//end function
    
    }//end class cart
    

    上述代码导致的错误如下:“致命错误:无法在写入上下文中使用函数返回值”。我在上面的代码中查找了很多次,但似乎没有找到错误。如果有人能帮忙,我将不胜感激。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Barmar    8 年前

    问题是这一行:

    $data('main_content') = 'cart';
    

    使用括号表示您正在调用函数,但不能分配给函数调用。您可能想分配给数组索引,但您没有 $data 大堆我看到你创造了 $数据 add() 函数,但该变量是该函数的局部变量。它们可能都应该是类属性,所以它们应该是 $this->data 。并且在 index() 您需要使用方括号。

       public function index(){
        //load view
        $this->data['main_content'] = 'cart';
        $this->load->view('layouts/main', $this->data);
       }//end function
    
       //add to Cart
       public function add(){
        //item data in a array construct
        $this->data = array('id'=> $this->input->post('item_number'), 'qty'=>   $this->input->post('qty'), 'price'=> $this->input->post('price'), 'name'=>  $this->input->post('title'));//end statement
    
       }//end function
    
        2
  •  0
  •   Rp9    8 年前

    此行可能有错误

    $data = array();
    $data['main_content'] = 'cart';
    
    推荐文章