Please I have a html form array with different keys name='qt[]'
, name='price[]'
, name='desc[]'
.
So I want to foreach this arrays and put the value in a html table how can I do?
loop
<tr>
<td><?php echo qt; ?></td>
<td><?php echo price; ?></td>
<td><?php echo desc; ?></td>
</tr>
end loop
This is my code :
foreach ($items['quantite'] as $qt) {
foreach ($items['description'] as $desc) {
foreach ($items['price'] as $price) {
But that give me a repeating.
Thank you!
Of course you do not get what you want when you nest loops.
You could use the $key => $value
syntax for looping over one of the arrays, and then inside the loop use the key to access the values from the other arrays. (Of course that’ll only work if all arrays have exactly the same structure.) … but:
i have a html form array with different keys name='qt[]', name='price[]', name='desc[]'
I’d suggest you rather change that – use qt
, price
and desc
as indices in the first place.
name="product[0][qt]", name="product[0][price]", name="product[0][desc]"
name="product[1][qt]", name="product[1][price]", name="product[1][desc]"
That will give you an array product
inside your $_POST (assuming your form uses POST), that you can simply loop over – and inside that loop, you can then access the entries qt
, price
and desc
for each product.
This has the advantage of “grouping” your data together by product – whereas with you current approach, if one of the values was to be missing (f.e. when one of the input fields was a checkbox, that would not get submitted when not checked), your whole system would fall apart and you would not be able to match the correct values to the correct product any more.