I am developing a website that can let the user buy something. The problem comes if the user buys many items at once.
For example: User A bought item 1 with quantity 2 User B bought item 2 with quantity 1
I want the confirmation page to display:
You have bought
Item 1, quantity: 2
Item 2, quantity: 1
The problem is that I can't find how to get each of the quantity based on the selected item. I am just able to display directly like this
You have bought
Item 1, quantity: 2 quantity 1
Item 2, quantity: 2 quantity 1
This is my user input form
echo "<input type=\"text\" hidden name=\"cart[]\" id=\"itemIDValue\" value=\" ".$itemID ."\"/>";
echo "<input type=\"text\" hidden name=\"qty[]\" value=\"".$itemQty."\">";
and this is my PHP code
$sql = "this will get all the data from database";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_array($result)){
echo"<p> Brand : " . $row[4] . " </p>
<p> Name : " . $row[3] ."</p>";
}
Basically I can get the item name and the brand correctly, but I can't get the quantity correctly for that particular item.
I tried to use foreach loop
while ($row = mysqli_fetch_array($result)){
foreach($qty as $a => $value){
echo"<p> Brand : " . $row[0] . " </p>
<p> Name : " . $row[1] ."</p>
<p> Qty : " . $value . "</p>";
}
}
The foreach loop will give me what I want, but now the problem is, if the user bought 2 items, the data output will be repeated and as a result there will be 4 items(2 items x2)
Can anybody help me please? Any help given is highly appreciated. Thanks