The foreach function in php will be used for a easy way to iterate over the loop and it is only used in array and objects or It will give you an error when you try to use it on a variable with different data type.
Syntax:
foreach(array_expression as $value)
<?php
if (isset($_GET['id']) && isset($_GET['product']) && isset($_GET['price']))
{
$id=$_GET['id'];
$Product=$_GET['product'];
$Price=$_GET['price'];
$arr = array('id'=>$id,'prod'=>$Product,'price'=>$Price);
$_SESSION['list'][]=$arr;
header('Location:cart.php');
}
?>
<?php
foreach($_SESSION['list'] as $value){
?>
<tr>
<td><?php echo $value['id']; ?></td>
<td><?php echo $value['prod']; ?> </td>
<td><?php echo $value['price']; ?> </td>
<tr>
<?php }
?>
This loop gives and array_expression as a $_SESSION['name'] on every iteration, the value of the current element is assigned to $value. Every loop the value of the current element is assigned to $value. The internal array pointer is increased by one to the next element. The foreach loop runs for the all elements of an array.
0 Comment(s)