我想使用 foreach 循环在我的会话中显示所有存储的数据。我无法展示它。谁能给我一个关于如何显示储值的想法。我需要显示每个添加产品的产品 ID 和信息。这是代码 - `
<?php
session_start();
/*
TEMPLATE NAME: Cart Page
*/
?>
<?php
$git_product_id = $_POST['git_product_id'];
$git_required = $_POST['git_required'];
$git_action = $_POST['git_action'];
if ( isset($git_product_id)){
switch($git_action){
case "add" :
// adding product
// checking first if the product is already added
if (isset($_SESSION['cart'][$git_product_id])){
echo "You have already added this product";
} else {
// I AM NOT SURE IF THIS CODING IS OKAY. PLEASE CHECK THIS
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
$_SESSION['cart'][$git_product_id] = array('product_id' => $git_product_id, 'info' => $git_required);
}
break;
case "remove":
// removing product
unset($_SESSION['cart'][$git_product_id]);
break;
case "empty" :
// empty cart
unset($_SESSION['cart']);
break;
}
}
?>
<?php
if ($_SESSION['cart'] != ""):
foreach($_SESSION['cart'] as $product => $key ) : ?>
<tr>
<td>
<?php // I WANT TO SHOW HERE EACH PRODUCT ID and RESPECTIVE REQUIRED INFO ?>
<?php // BUT I DON'T KNOW HOW TO DO IT ?>
</td>
<td>
<form action="" method="post">
<input type="hidden" name="git_product_id" value="<?php echo $product; ?>" />
<input type="hidden" name="git_required" value="<?php echo $qty; ?>" />
<input type="hidden" name="git_action" value="remove" />
<input type="submit" value="Remove" />
</form>
</td>
<?php endforeach; ?>
<?php endif; ?>
<form action="" method="POST">
<input type="hidden" name="git_product_id" value="<?php echo $product; ?>" />
<input type="hidden" name="git_required" value="<?php echo $qty; ?>" />
<input type="hidden" name="git_action" value="empty" />
<input type="submit" value="empty" />
</form>