数组变量未设置 - PHP Codeigniter

时间:2021-02-17 21:38:19

I am trying to set array, but it is not getting set. Everytime the Buy function called, the array gets declared.

我正在尝试设置数组,但它没有设置。每次调用Buy函数时,都会声明数组。

This is the function is controller.

这是功能是控制器。

public function buy() {
    if($this->session->userdata('counter')){
        $counter = $this->session->userdata('counter');
        $this->session->set_userdata('counter', $counter + 1);
    } else {
        $this->session->set_userdata('counter', 1);

    }
if(isset($bought)){
        $name = $this->input->post('name');
        $price = $this->input->post('price');
        $qty = $this->input->post('qty');
        $product = array('name' => $name, 'price' => $price, 'qty'=> $qty);
        array_push($bought, $product);
        var_dump($bought);
        die();
    } else {
        $bought = array();

    redirect("");
    }

As you can see, it should remember that $bought is set, but it gets declared anew. For now I

正如你所看到的,它应该记住$ buy已设置,但它会被重新声明。现在我

  • tried to make it global, before "public function __construct()",
  • 在“public function __construct()”之前试图使它成为全局的,

  • tried "if (!empty),
  • 试过“if(!empty),

  • tried to put into session,
  • 试图投入会议

  • tried to find the answer across whole *...
  • 试图在整个*中找到答案......

Please let me know if additional info needed. Thanks a lot!

如果需要其他信息,请告诉我。非常感谢!

2 个解决方案

#1


In your code when you are checking for $bought, it is not present so it is going to the else part everytime. Instead define a blank array and then set the values inside it. Try with -

在您检查$ buy时的代码中,它不存在,因此每次都会进入else部分。而是定义一个空白数组,然后在其中设置值。尝试 -

if(isset($_SESSION['bought'])){
    $name = $this->input->post('name');
    $price = $this->input->post('price');
    $qty = $this->input->post('qty');
    $product = array('name' => $name, 'price' => $price, 'qty'=> $qty);
    array_push($_SESSION['bought'], $product);
    var_dump($_SESSION['bought']);
    die();
} else {
    $_SESSION['bought'] = array();
}

And start the session at top of the page.

并在页面顶部开始会话。

#2


I got it working.

我搞定了。

I made a workaround with sgt's help, so now my code looks like this:

我用sgt的帮助做了一个解决方法,所以现在我的代码看起来像这样:

if($this->session->userdata('bought')){
        $name = $this->input->post('name');
        $price = $this->input->post('price');
        $qty = $this->input->post('qty');
        $product = array('name' => $name, 'price' => $price, 'qty'=> $qty);

        // please see that here I just returned from old one and added new data to the old one. 
        // Then I just added the result to the session.
        $old_session =  $this->session->userdata('bought');
        array_push($old_session, $product);
        $this->session->set_userdata('bought', $old_session);

        redirect("");
    } else {
        $this->session->set_userdata('bought', array());
}

#1


In your code when you are checking for $bought, it is not present so it is going to the else part everytime. Instead define a blank array and then set the values inside it. Try with -

在您检查$ buy时的代码中,它不存在,因此每次都会进入else部分。而是定义一个空白数组,然后在其中设置值。尝试 -

if(isset($_SESSION['bought'])){
    $name = $this->input->post('name');
    $price = $this->input->post('price');
    $qty = $this->input->post('qty');
    $product = array('name' => $name, 'price' => $price, 'qty'=> $qty);
    array_push($_SESSION['bought'], $product);
    var_dump($_SESSION['bought']);
    die();
} else {
    $_SESSION['bought'] = array();
}

And start the session at top of the page.

并在页面顶部开始会话。

#2


I got it working.

我搞定了。

I made a workaround with sgt's help, so now my code looks like this:

我用sgt的帮助做了一个解决方法,所以现在我的代码看起来像这样:

if($this->session->userdata('bought')){
        $name = $this->input->post('name');
        $price = $this->input->post('price');
        $qty = $this->input->post('qty');
        $product = array('name' => $name, 'price' => $price, 'qty'=> $qty);

        // please see that here I just returned from old one and added new data to the old one. 
        // Then I just added the result to the session.
        $old_session =  $this->session->userdata('bought');
        array_push($old_session, $product);
        $this->session->set_userdata('bought', $old_session);

        redirect("");
    } else {
        $this->session->set_userdata('bought', array());
}