Above is what I'm trying achieve in my code.
*P=Product
Below is my code. All I need to do is to display the 'id' as well as 'qty' using foreach.Now it only displays key 'id' and it's value.
I tried
$_SESSION['items'][]=array('id'=>$id,'qty'=>$qty);
instead of
$_SESSION['items']=array('id'=>$id,'qty'=>$qty);
BUT I'm getting this error:Problem with: Fatal error: [] operator not supported for string..
PHP
<?php
session_start();
?>
<?php
$id=$_GET['id'];
$qty=$_GET['qty'];
$_SESSION['items']=array('id'=>$id,'qty'=>$qty);
print_r($_SESSION['items']);
foreach($_SESSION['items'] as $items=>$value)
{
echo $items;
echo $value;
}
?>
Edited :
I manage to solve the issue. However now,I'm trying to include the above for each product. I mean,
I'm getting the data like this:
$id = $_GET['id'];
$qty = $_GET['qty'];
$product_id=$_GET['product_id'];
//store details inside session
$_SESSION['items'][]= array('id'=>$id,'qty'=>$qty,'product_id'=>$product_id);
print_r($_SESSION['items']);
$item_id= explode(',',$_SESSION['items']['id']);
$item_qty= explode(',',$_SESSION['items']['qty']);
$item_product= explode(',',$_SESSION['items']['product_id']);
//retrive info from database
mysql_select_db($database_wedding_conn, $wedding_conn);
$select="SELECT tbl_additional_info.*,tbl_productscontents.* FROM
tbl_additional_info,tbl_productscontents WHERE tbl_additional_info.productid='$product_id AND tbl_additional_info.product_id=tbl_productscontents.product_id GROUP BY tbl_productscontents.productid'";
$result=mysql_query($select)or die(mysql_error());
THis is how it displays in table
<table>
<tr>
<th>Product</th>
<th>Size</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
<?php
foreach($item_product as $p=>$p_value)
{
?>
<tr>
<td valign="top">
<?php
echo $p;
echo $p_value;
?>
</td>
<td>
<table>
<?php
foreach($item_id as $i)
{
?>
<tr>
<td>
<?php echo $i;?>
</td>
</tr>
<?php
}
?>
</table>
</td>
<td>
<?php
foreach($item_qty as $q)
{
//unit price
// echo $q;
}
?>
</td>
<td>
<table>
<?php
foreach($item_qty as $q)
{
?>
<tr>
<td>
<?php echo $q;?>
</td>
</tr>
<?php
}
?>
</table>
</td>
<td>
<?php
foreach($item_qty as $q)
{
//amount
//echo $q;
}
?>
</td>
</tr>
<?php
}
?>
</table>
Question: How to I display all the product stored in the session? The for each loop only shows one product.It doesn't add more as it shows in print_r($_SESSION['items'])