0

私のタイトルとして、そのメソッドを呼び出そうとしましたが、エラーが発生しました:

Fatal error: Call to a member function post() on a non-object in C:\xampp\htdocs\cifirst\application\modules\front\controllers\shopping.php on line 11

モジュールではないコントローラーを作成すると、そのメソッドは非常に簡単に使用できますが、この場合は使用できません (以下のメソッドのすべてのコードは実行できません)。これは私のコードです:

 public function add_to_cart() {
  $data = array(
   'id' => $this->input->post('productId'), // line 11
   'name' => $this->input->post('productName'),
   'price' => $this->input->post('productPrice'),
   'qty' => 1,
   'options' => array('img' => $this->input->post('productImg'))
  );

  $this->load->library('MY_Cart');
  $this->cart->insert($data);

  //redirect($_SERVER['HTTP_REFERER']);
  //echo $_POST['productId'].'-'.$_POST['productName'];
 }

そして、このコードも機能しません:

public function __construct() {
    $this->load->library('cart');
    $this->load->helper('form');
}

XAMPP 1.8.1、CodeIgniter 2.1.3、および最新の MX を使用しています。私を助けてください!

4

2 に答える 2

1

あなたが電話している場合:

$this->input->post('productId');

問題はコンストラクター宣言またはクラス名にあるよりもコントローラー内にあります。コンストラクト部分には次のようなコードが含まれている必要があります。

Class Home extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->CI->load->library('cart');
        $this->CI->load->helper('form');
    }


     public function add_to_cart() 
     {
          $data = array(
                  'id' => $this->input->post('productId'), // line 11
                  'name' => $this->input->post('productName'),
                  'price' => $this->input->post('productPrice'),
                  'qty' => 1,
                  'options' => array('img' => $this->input->post('productImg'))
                  );


      }
}

ヘルパー関数または他のクラスから呼び出している場合は、次のようにする必要があります。

  function __construct()
  {
        parent::__construct();
        $this->CI->load->library('cart');
        $this->CI->load->helper('form');

        $this->CI =& get_instance();
  } 


  public function add_to_cart() 
  {
     $data = array(
        'id' => $this->CI->input->post('productId'), // line 11
        'name' => $this->CI->input->post('productName'),
        'price' => $this->CI->input->post('productPrice'),
        'qty' => 1,
        'options' => array('img' => $this->CI->input->post('productImg'))
        );
 }
于 2013-07-13T04:11:32.880 に答える