我希望有人能帮助我。我不明白如何使用会话将产品信息从我的产品页面传递到我的购物车页面。我已经看过很多教程,展示了如何在同一页面中使用它,但是在页面之间传递它没有什么可以理解的。感谢您的帮助!
这是我的产品页面,带有数据文件连接。
<?php
session_start();
$data = file('products.dat');
foreach ($data as $line) {
$products = explode(",", $line);
$id = $products[0];
$product = $products[1];
$description = $products[2];
$price = $products[3];
$image = $products[4];
$_SESSION['cart'] = array($id, $product, $price);
?>
<div class="product">
<?php print "<image class="product_image" src="img/$image">" ?><br>
<?php print $id; ?><br>
<?php print $title; ?><br>
<?php print $description; ?><br>
<?php print $price; ?>
<form method="get" action="viewcart.php?action=addcart">
<div>
<input type="hidden" name="product" value="<?php print $product?>" />
<input type="hidden" name="price" value="<?php print $price?>" />
<button type="submit">Add To Cart</button>
</div> <br>
</form>
</div>
<?php }; ?>
还有我的购物车页面。
<?php
session_start();
?>
<table border="1" cellspacing="0" cellpadding="10" width="100%">
<tr>
<th class="left">Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td class="left"><?php print $_SESSION['cart'][1]; ?></td>
<td class="center">1</td>
<td class="center"><?php print $_SESSION['cart'][2]; ?></td>
</tr>
</table>
编辑:我在每个页面上都抛出了一个session_start()
并编辑了我的产品页面,以便在我的初始foreach循环中有一个$_SESSION['cart']
,当测试时,它将打印出每个项目的正确信息。新的问题是,当我向购物车添加某些内容时,GET
通过URL发送正确的信息,但只会显示产品数组中的最后一项。这里有什么建议吗?
我没有在每个页面上看到任何session_start()
函数。没有它,什么都行不通。