I'm currently working on an eCommerce app where I am working with a SESSION cart. In this cart, I have two items possible to sell. If I add the first item, it adds signifies the item correctly for my records, however if the second item is the only item selected, it's value takes the place of the the assignment I have for the first value.
To give a better understanding of what I mean, here is some code:
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
$output .= '
<div class="customer_2_item_line">
<div class="customer_2_item_line_cell"><img src="image/'.$values['product_code'].'.jpg"><span class="customer_2_line_cell_amt">'.$values['product_quantity'].'</span></div>
<div class="customer_2_item_line_cell"><p>ET Spork</p><p><span></span><span class="customer_2_line_cell_name">'.$values['product_name'].'</span<</p></div>
<div class="customer_2_item_line_cell"><p><span>$</span><span class="customer_2_line_cell_total">'.$values['product_total'].'</span></p></div>
</div>
<hr>
';
$total_price = number_format($total_price + ($values['product_quantity'] * $values['product_price']), 2);
$total_item = $total_item + $values['product_quantity'];
$total_price_shipping = number_format($total_price + $shipping_price, 2);
$qty[] = $values['product_quantity'];
}
$output .= '
<input type="text" id="subtotal_cost" name="subtotal_cost" value='.$total_price.'>
<input type="text" id="shipping_total_cost" name="shipping_total_cost" value='.$shipping_price.'>
<input type="text" id="total_cost" name="total_cost" value='.$total_price_shipping.'>
<input type="hidden" id="item_qty1" name="item_qty1" value='.$qty[0].'>
<input type="hidden" id="item_qty2" name="item_qty2" value='.$qty[1].'>
<div id="customer_2_subtotal_shipping">
<div class="customer_2_ss_line"><p>Subtotal</p><p><span>$</span><span></span></p></div>
<div class="customer_2_ss_line"><p>Shipping</p><p><span>$</span><span></span></p></div>
</div>
<hr>
<div id="customer_2_total">
<p>Total</p>
<p><span>USD</span><span><span>$</span><span></span></span></p>
</div>
';
}
I have my $qty[] array for each cart item, but it only works if both items are in the cart. I can't figure out how to organize them so that each qty has a unique value to its product during the loop so that no matter which item is being ordered, it is assigned to it's respective product name and not by the order in which it is being purchased.
The problem is that if there is no second item being added to the cart, it only reads the first value, and then my cart gets mismatched to my fixed 'name' values to 'quantity' values of whichever quantity there is to retrieve or comes first.
Any help would be greatly appreciated.