Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php cart code for adding items loop / array

Status
Not open for further replies.

Eleanor4

Programmer
May 17, 2010
1
GB
Hi,

I'm fairly new to PHP and really need some help with the following code.

I need some kind of loop or array that will help me add multiple shopping cart items to a form.

This is the code I am currently using...

Code:
<?  foreach ($invoice->cart->items as $id => $item) {
?>
	<input type="hidden" name="item_name_1" value="<?=$item->name?>">
	<input type="hidden" name="item_number_1" value="<?=$id?>">
	<input type="hidden" name="quantity_1" value="<?=$item->qty?>">
	<input type="hidden" name="amount_1" value="<?=number_format($item->price, 2)?>">	
 	<?  } ?>

The issue is that for each item in the shopping cart I need the name of the form field to increase by 1.

So if if there were 3 items in the shopping cart the form would need to display like this

Code:
<input type="hidden" name="item_name_1" value="yellow food box">
	<input type="hidden" name="item_number_1" value="SKU023">
	<input type="hidden" name="quantity_1" value="1">
	<input type="hidden" name="amount_1" value="85.00">	


<input type="hidden" name="item_name_2" value="Red food box">
	<input type="hidden" name="item_number_2" value="SKU012">
	<input type="hidden" name="quantity_2" value="1">
	<input type="hidden" name="amount_2" value="85.00">



<input type="hidden" name="item_name_3" value="Black food box">
	<input type="hidden" name="item_number_3" value="SKU035">
	<input type="hidden" name="quantity_3" value="1">
	<input type="hidden" name="amount_3" value="85.00">


Does that make sense?

Really appreciate any help

Thanks

Elle
 
this should work

Code:
$count = 0;
foreach ($invoice->cart->items as $id => $item) {
    $price = number_format($item->price, 2);
    echo <<<HTML
    <input type="hidden" name="item_name_{$count}" value="{$item->name}">
    <input type="hidden" name="item_number_{$count}" value="{$id}">
    <input type="hidden" name="quantity_{$count}" value="{$item->qty}">
    <input type="hidden" name="amount_{$count}" value="{$price}">
HTML;
   $count++;
 }
 
It looks like you are attempting to output the contents of the cart into a form and allow updating the quantities - correct? If so, I would only make the quantity field as a form field. Providing hidden fields to a browser that you would use on the next page opens up more possibilities to hijack the data (ie changing the price) - avoid this if possible.

jpadie's suggestion would work though an alternative would be to use an array.
Code:
<?  foreach ($invoice->cart->items as $id => $item) {
?>
    <input type="hidden" name="item_name[<?=$id?>]" value="<?=$item->name?>">
    <input type="hidden" name="quantity[<?=$id?>]" value="<?=$item->qty?>">
    <input type="hidden" name="amount[<?=$id?>]" value="<?=number_format($item->price, 2)?>">    
     <?  } ?>
Then in post you could easily check it via the ID:
Code:
$_POST['quantity'][$id]
 
i believe the OP is using an online payment gateway that requires information to be POSTED to it using the notation shown in the OP's original post. from memory an old version of the paypal spec required this. array notation was not supported.
 
I've never worked with paypal so I don't know their specs, but that would be an example of not being able to avoid it.

At one point we had a cart gateway that was implemented like that and it was ridiculously easy to modify the price I wanted to pay for an item - which one I pointed out to the boss then wanted to stop using them.

Just thought I'd throw out the warning.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top