数组变量未设置 - PHP Codeigniter
Array variable is not getting set - PHP Codeigniter
我正在尝试设置数组,但未设置。每次调用 Buy 函数时,都会声明数组。
这是控制器的函数。
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("");
}
如您所见,它应该记得 $bought 已设置,但它会重新声明。现在我
- 在 "public function __construct()"、
之前尝试使其成为全球性的
- 尝试过“如果(!空),
- 试图进入会话,
- 试图在整个 Whosebug 中找到答案...
如果需要更多信息,请告诉我。
非常感谢!
在您检查 $bought
的代码中,它不存在,因此它每次都会转到 else
部分。而是定义一个 blank array
然后在其中设置值。试试 -
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();
}
并在页面顶部开始会话。
我成功了。
我在 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());
}
我正在尝试设置数组,但未设置。每次调用 Buy 函数时,都会声明数组。
这是控制器的函数。
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("");
}
如您所见,它应该记得 $bought 已设置,但它会重新声明。现在我
- 在 "public function __construct()"、 之前尝试使其成为全球性的
- 尝试过“如果(!空),
- 试图进入会话,
- 试图在整个 Whosebug 中找到答案...
如果需要更多信息,请告诉我。 非常感谢!
在您检查 $bought
的代码中,它不存在,因此它每次都会转到 else
部分。而是定义一个 blank array
然后在其中设置值。试试 -
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();
}
并在页面顶部开始会话。
我成功了。
我在 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());
}