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 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。