dongshan4878 2018-03-23 08:33
浏览 41
已采纳

我的CI_Controller中的写上下文中不能使用函数返回值

    <?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

The above code causes error as follows:"Fatal error: Cant' use function return value in write context". I looked and searched many time at the above code but just can not seem to find the error. I would greatly appreciate it if someone can help.

  • 写回答

2条回答 默认 最新

  • douqianxian7008 2018-03-23 08:39
    关注

    The problem is this line:

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

    Using parentheses means you're calling a function, but you can't assign to a function call. You probably meant to assign to an array index, but you have no $data array. I see you've created $data in the add() function, but that variable is local to that function. They probably both should be class properties, so they should be $this->data. And in index() you need to use square brackets.

       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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog