【发布时间】:2017-11-25 02:27:41
【问题描述】:
好的,所以我在使用购物车库的地方有这个 Codeigniter 代码。现在,我的问题是我的 if 似乎不适用于控制器中的特定功能。我想检查用户当前的积分是否大于商品的价格,它会将商品添加到购物车中,如果小于则它会重定向到登录页面(仅作为测试)。但不知何故,它只是不会承认该 If 语句的存在并完全忽略它。有什么想法吗?
控制器
function go_addtocart_user($user)
{
$id=$this->input->post('id_things');
$price=$this->input->post('price_things');
$name=$this->input->post('name_things');
$getalluser=$this->user->get_credit_user($user);
foreach ($getalluser as $row){
if($row>=$price)
{
$this->user->decrease_things_stock($name);
$this->user->decrease_credit($user,$price);
$this->user->insert_to_cart($id,$price,$name);
}
else{
redirect("main/go_login/");
}
}
redirect("main/go_home/".$user);
}
型号
public function get_credit_user($name){
$this->db->select('credit');
$this->db->from('user');
$this->db->where('username', $name);
$query = $this->db->get();
$result=$query->result_array();
return $result;
}
public function decrease_credit($username,$price){
$this->db->where('username', $username);
$this->db->set('credit', 'credit - ' . (int) $price, FALSE);
$this->db->update('user');
return true;
}
public function decrease_things_stock($name){
$this->db->where('name', $name);
$this->db->set('stock', 'stock-1', FALSE);
$this->db->update('things');
return true;
}
public function insert_to_cart($id,$price,$name){
$data = array(
'id' => $id,
'name' => $name,
'qty' => 1,
'price' => $price
);
$this->cart->insert($data);
}
【问题讨论】:
-
$row的值和类型是什么?在我看来,这将是一个数组或一个对象,不会与$price进行逻辑比较。 -
我看到的是,无论 if/else 语句做什么,您都要求在每个循环结束时进行另一个重定向:
redirect("main/go_home/".$user);所以这不能满足您对登录页面的需求重定向。 -
由于
get_credit_user()返回一个数组->$result=$query->result_array(); return $result;,你需要得到credit的值->if($row['credit']>=$price) -
@Sean 好吧,我会被诅咒的,这确实有效。非常感谢:)
标签: php codeigniter