0
如何從foreach循環獲取數組索引,我使用session來打印購物車數據,我需要在代碼列之前打印索引,如1,2,3,4表如何從foreach循環獲取數組索引
<?php
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<tbody>';
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<tr>';
echo '<td>?</td>';
echo '<td>'.$cart_itm["code"].'</td>';
echo '<td>'.$cart_itm["name"].'</td>';
echo '<td>'.$cart_itm["qty"].'</td>';
echo '<td><input type="text" name="product_qty_desired" class="spinner" value="1" size="3" /></td>';
echo '<td>'.$currency.$cart_itm["price"].'</td>';
echo '<td class="subtotal">Subtotal : </td>';
echo '<td><span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span></td>';
echo '</tr>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '<tr>';
echo '<td></td>';
echo '<td></td>';
echo '<td>Total QTY :</td>';
echo '<td></td>';
echo '<td>?</td>';
echo '<td>Total</td>';
echo '<td>'.$currency.$total.'</td>';
echo '<td></td>';
echo '</tr>';
echo '</tbody>';
}else{
echo 'Your Cart is empty';
}
?>
http://i62.tinypic.com/2qaifrt.png
感謝
熱烈
Thanks @blaatpraat – 2015-03-03 11:14:38