Reputation: 611
Struggling a little on this one.
This is my foreach code
<?php
$subtotal = 0;
foreach ($go_cart['contents'] as $cartkey=>$product):?>
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
<?php endforeach; ?>
And for this line
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
I need it to up by one for each foreach. Such as:
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
<td><input type="hidden" name="itemdescription_1" value="Description" /></td>
<td><input type="hidden" name="itemdescription_2" value="Description" /></td>
I'm pretty sure you have the quick solution for me on this one...
Thanks guys!, appreciate it.
HERE IS WHAT I FORGOT TO MENTION:
I have multiple fields for this action. So i have description, itemcount, itemamount etc.
So they should be like this
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
<td><input type="hidden" name="itemcount_0" value="Description" /></td>
<td><input type="hidden" name="itemamount_0" value="Description" /></td>
But with the solution you sent me i get.
<td><input type="hidden" name="itemdescription_1" value="Description" /></td>
<td><input type="hidden" name="itemcount_2" value="Description" /></td>
<td><input type="hidden" name="itemamount_3" value="Description" /></td>
Sorry, my mistake!...
Upvotes: 2
Views: 129
Reputation: 15044
Change:
$subtotal = 0;
to
$subtotal = 0;
$i = 0;
Then do the following:
<td><input type="hidden" name="itemdescription_<?php echo $i; ?>" value="Description" /></td>
<td><input type="hidden" name="itemcount_<?php echo $i; ?>" value="Description" /></td>
<td><input type="hidden" name="itemamount_<?php echo $i; ?>" value="Description" /></td>
Right BEFORE you close your outer foreach()
loop do this on its own line:
$i++;
What you are doing is first initializing the variable $i
by setting it to 0
. You then output the current value of $i
. Then you will use the post-increment operator with $i
as $i++
at the end of your loop to increase its value by 1
so the next time it is output on next run of the loop it will be increased by 1
.
Upvotes: 5
Reputation: 2334
Here's the code
<?php
for ($i=0; $i<=2; $i++)
{
echo "<td><input type=\"hidden\" name=\"itemdescription_" . $i . "value=\"Description\" /></td>"."\n";
}
?>
Upvotes: 0