购物车系统中的 foreach 和 while 错误
foreach and while errors in cart system
我正在使用 paypal 编写购物车系统我遇到了将不同产品设置为 paypal 形式的 foreach 的问题这是代码:
public function GetUserCart(){ //we get the items in the user cart
$mysqli = $this->connection();
$data = $mysqli->query("SELECT * FROM carrito WHERE usuario_id='$this->username_id'");
return $data;
}
//we get the values of the products
public function GetPictureData($id_product,$picture_column){
$mysqli = $this->connection();
$validacion = $mysqli->query("SELECT * FROM pinturas WHERE id='$id_product' LIMIT 1");
$data = $validacion->fetch_assoc();
return $data[$picture_column];
}
$itemstopaypalname=''; //name of the product
$itemstopaypalprice=''; //price of the product
//we get the items in the user cart
$querys = $this->GetUserCart();
$dataset = array(); //this is for use outside a while
while ($data = $querys->fetch_array()) {
$dataset[] = $data;
}
$cart .= '<input type="hidden" name="notify_url" value="'.$this->url.'files/libs/paypal.php">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="'.$this->email().'">';
$num = 1;
foreach($dataset as $k => $v){ //this is where im having troubles
$itemstopaypalname.=$this->GetPictureData($v['producto_id'],"titulo");
$itemstopaypalprice.=$this->GetPictureData($v['producto_id'],"price");
$cart .= '<input type="hidden" name="item_name_'.$num.'" value="'. $itemstopaypalname.'">';
$cart .= '<input type="hidden" name="amount_'.$num.'" value="'. $itemstopaypalprice.'">';
$num = $num + 1;
}
所以当第二个和第三个产品分别重复第一个和第二个产品时:
<input type="hidden" name="item_name_1" value="mono">
<input type="hidden" name="amount_1" value="567">
<input type="hidden" name="item_name_2" value="monocosos">
<input type="hidden" name="amount_2" value="5674678">
<input type="hidden" name="item_name_3" value="monocososImage Upload">
<input type="hidden" name="amount_3" value="56746782450">
所以你可以看到每个数量都有 567 次重复,我该如何解决这个问题?
$itemstopaypalname.=
这也将连接以前的字符串。
尝试
$itemstopaypalname=$this->GetPictureData($v['producto_id'],"titulo"); $itemstopaypalprice=$this->GetPictureData($v['producto_id'],"price");
我正在使用 paypal 编写购物车系统我遇到了将不同产品设置为 paypal 形式的 foreach 的问题这是代码:
public function GetUserCart(){ //we get the items in the user cart
$mysqli = $this->connection();
$data = $mysqli->query("SELECT * FROM carrito WHERE usuario_id='$this->username_id'");
return $data;
}
//we get the values of the products
public function GetPictureData($id_product,$picture_column){
$mysqli = $this->connection();
$validacion = $mysqli->query("SELECT * FROM pinturas WHERE id='$id_product' LIMIT 1");
$data = $validacion->fetch_assoc();
return $data[$picture_column];
}
$itemstopaypalname=''; //name of the product
$itemstopaypalprice=''; //price of the product
//we get the items in the user cart
$querys = $this->GetUserCart();
$dataset = array(); //this is for use outside a while
while ($data = $querys->fetch_array()) {
$dataset[] = $data;
}
$cart .= '<input type="hidden" name="notify_url" value="'.$this->url.'files/libs/paypal.php">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="'.$this->email().'">';
$num = 1;
foreach($dataset as $k => $v){ //this is where im having troubles
$itemstopaypalname.=$this->GetPictureData($v['producto_id'],"titulo");
$itemstopaypalprice.=$this->GetPictureData($v['producto_id'],"price");
$cart .= '<input type="hidden" name="item_name_'.$num.'" value="'. $itemstopaypalname.'">';
$cart .= '<input type="hidden" name="amount_'.$num.'" value="'. $itemstopaypalprice.'">';
$num = $num + 1;
}
所以当第二个和第三个产品分别重复第一个和第二个产品时:
<input type="hidden" name="item_name_1" value="mono">
<input type="hidden" name="amount_1" value="567">
<input type="hidden" name="item_name_2" value="monocosos">
<input type="hidden" name="amount_2" value="5674678">
<input type="hidden" name="item_name_3" value="monocososImage Upload">
<input type="hidden" name="amount_3" value="56746782450">
所以你可以看到每个数量都有 567 次重复,我该如何解决这个问题?
$itemstopaypalname.=
这也将连接以前的字符串。
尝试
$itemstopaypalname=$this->GetPictureData($v['producto_id'],"titulo"); $itemstopaypalprice=$this->GetPictureData($v['producto_id'],"price");