【发布时间】:2011-09-27 18:09:06
【问题描述】:
所以我有一个可以正常工作的购物车,但我想在 for each 循环之外的联系表单中显示购物车的结果。
我遇到的问题是,每当我尝试显示 $Ptitle 时,它只会显示购物车的最后添加内容。不是数组内的完整标题列表。我尝试了各种方法来显示数组结果,但似乎都不起作用。看来我可以通过
打印数组print_r ($contents);
但仅限于 for each 循环内部,而不是外部。 下面是代码,如有乱码请见谅。任何帮助将不胜感激。
谢谢!
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1: 1;
}
echo '<form action="http://www.changecompanies.net/dev/theme/cart_checkout.php?action=update" method="post" id="cart">';
//$output[] = '<table>';
?>
<table id="cart">
<tr>
<th>Quantity</th>
<th></th>
<th>Item</th>
<th>Product Type</th>
<th>Unit Price</th>
<th>Total</th>
<th>Remove</th>
</tr>
<?php
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM tccproducts WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$result = mysql_query("SELECT * FROM tccproducts WHERE Pid ='$id'")or die (mysql_error());
//if (!$result);
//echo "no result";
$myrow = mysql_fetch_array($result) or mysql_error('error!');
$name = $myrow['Ptitle'];
//$name = substr($name,0,40);
$Pprice = $myrow['Pprice'];
$Ptitle = $myrow['Ptitle'];
$Pimage = $myrow['Pimage'];
$Pcat = $myrow['Pcategory'];
$mini = $myrow['Pminimum'];
Rest 只显示购物车并结束 for each 循环。
【问题讨论】:
-
为什么不能将数组转储到 foreach 循环之外?
-
这是我的问题星际......我可以显示数组,但我不知道如何从中获取正确的值......例如我可以显示“Pids”A9, A9,A9,B12,B12,B12 一样,如果每个有 3 个产品。但是我如何挖掘这些数字并将购物车上列出的标题、价格以及基本上所有其他内容显示到电子邮件消息中?
标签: php arrays shopping-cart