foreach和推车系统中的错误

时间:2015-01-30 05:49:30

标签: php loops paypal

我正在使用paypal编程购物车系统我遇到麻烦的foreach将不同的产品设置为paypal形式这是代码:

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个重复,我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

$itemstopaypalname.=这也将连接以前的字符串。

尝试

$itemstopaypalname=$this->GetPictureData($v['producto_id'],"titulo"); $itemstopaypalprice=$this->GetPictureData($v['producto_id'],"price");