我正在使用URL中的添加項將項目添加到我的cart.php。PHP SESSION HTML表格攜帶問題
case "add":
if (isset($_SESSION['cart'][$comic_id])) {
$_SESSION['cart'][$comic_id]++;
} else {
$_SESSION['cart'][$comic_id] = 1;}
...我的項目都爲在cart.php一個HTML表格。我只是想在checkout.php中重新調用數組中的特定變量,以便客戶可以確認訂單總額以及他們的個人詳細信息。
我只能似乎在使用會話變量進行最後添加的項目/行,當我使用以下命令:
$_SESSION['totalnameqty']=$name . " " . $qty . " " . $cost;
...然後使用checkout.php頁面上的回聲:
$totnamqty=$_SESSION['totnamqty'];
echo $totnamqty;
...我想將所有項目$ name,$ qty & $成本添加到cart.php中的HTML表格中,而不是隻添加1個項目/行。不確定如何做到這一點,或者如果可能的話。有人可以幫忙嗎?
這裏是我的cart.php:
if (isset($_SESSION['cart'][$comic_id])){
echo "<table border=\"0\" padding=\"10\" width=\"80%\">";
echo "<td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";
echo "<td colspan=\"6\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Crystal Fusion: Are you sure you wish to empty your cart?');\">Empty Cart</a></td>";
echo "<tr height=\"20px\">";
echo "<tr height=\"20px\">";
echo "<td align=center>Image</td><td align=center>Title</td><td align=center>Description</td><td colspan=3 align=center>Copies (+/-)</td><td align=center>Price</td>";
echo "<tr height=\"20px\">";
foreach($_SESSION['cart'] as $comic_id => $qty) {
$sql = sprintf("SELECT title, description, cost, image_thumbnail
FROM comic
WHERE comic_id = %d;",$comic_id);
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
list($name, $description, $price, $image_thumbnail) = mysql_fetch_row($result);
$cost = $price * $qty;
$total = $total + $cost;
$cost = number_format($cost,2);
$total = number_format($total,2);
$description = substr($description, 0, 250);
echo "<br><tr>";
echo "<td width=\"10px\" align=\"center\"><img height=100 align=center src=\"$image_thumbnail\">";
echo "<td align=\"center\">$name</td>";
echo "<td width=\"40%\" align=\"center\">$description...<a href=comic_dyn.php?comic_id=$comic_id>More Info</td>";
echo "<td width=\"30px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=add&comic_id=$comic_id\">+<br></a><td align=\"center\">$qty <td width=\"20px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=remove&comic_id=$comic_id\">-</a></td>";
echo "<td align=\"right\">$$cost</td>";
echo "</tr>";
}
}
echo "<br><tr><tr height=100px>";
echo "<td><td><td colspan=\"4\" align=\"right\">Total:</td>";
echo "<td width=\"60px\" align=\"right\">$$total</td>";
echo "<tr><td colspan=\"7\" align=\"right\"><a href=\"checkout_html.php\">Proceed to Checkout</a>";
echo "<tr height=\"50px\">";
echo "</table>";
}else{
echo "Your cart is currently empty.";
echo "<br><br><td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";
}
//session variables (to be carried to checkout.php
$_SESSION['cost']=$cost;
$_SESSION['name']=$name;
$_SESSION['qty']=$qty;
$_SESSION['totnamqty']=$name . " " . $qty . " " . $cost;