我建立了自己的购物车,我需要将购物车中的所有产品ID转换为一个字符串。
修改
我可能不清楚。
见我的来源:
$sub_total=0;
foreach ($_SESSION['cart'] as $id => $value) {
$query = mysql_query("SELECT * FROM store_products WHERE product_id ='$id'");
while ($rows = mysql_fetch_array($query)){
if($rows['product_id']==$id){
$product_title=$rows['product_title'];
$product_id=$rows['product_id'];
$quantity=$_SESSION['cart'][$id]['quantity'];
$price=$rows['product_price'];
$total_price=$price*$quantity;
$sub_total=$sub_total+$total_price;
/*======PRODUCT URL============*/
$product_category_id=$rows['product_category_id'];
$query=mysql_query("SELECT * FROM store_categories WHERE category_id = '$product_category_id'");
$rows2=mysql_fetch_array($query);
$category_url=$rows2['category_url'];
$product_url=$rows['product_url']
?>
<tr>
<td style="padding-right: 30px;"><a href="<?php echo "/store/$category_url/$product_url";?>"><?php echo $product_title;?></a></td>
<td style="padding-right: 30px;">€ <?php echo $price;?></td>
<td style="padding-right: 30px;">€ <?php echo $total_price;?></td>
<td style="padding-right: 30px">
<a title="-1" href="/cartquantitydown/<?php echo $product_id;?>" style="color: red;text-decoration: none;">< </a>
<?php echo $quantity;?>
<a title="+1" href="/cartquantityup/<?php echo $product_id;?>" style="color: green;text-decoration: none;"> ></a>
<a href="/cartremoveitem/<?php echo $rows['product_id'];?>"><img title="Remove item" class="img" style="height: 20px;" src="/images/store/remove-icon.png"></a>
</td>
</tr>
<?php
}
}
}
我使用
将产品添加到购物车 $_SESSION['cart'][$product_id]['quantity']++;
那么如何才能将所有产品id和qunatity放到一个字符串中?
无论如何,如果你知道我的意思,我可以在这段代码之外放置一个数组吗?
答案 0 :(得分:2)
使用implode
$productids = array(
'6_41',
'4_645',
'1_534',
'1_5',
'1_635',
'1_124'
);
echo implode('-', $productids); // "6_41-4_645-1_534-1_5-1_635-1_124"
答案 1 :(得分:0)
连接它们:
$productids = $productid1 . "-" . $productid2