misslois74
Programmer
im working on a page in which im using an array stored in a session now my problem is everytime i click on F5 or refresh the page the value that is stored on that session is increasing eventhough i didnt click on submit....
here is my sample code:
here is my sample code:
Code:
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
$quer = "Select * from tbl_candle";
$result = mysql_query($quer);
while($row = mysql_fetch_array($result))
{
$sku = trim($row[product_id]);
$CATALOG[$sku][prod_id] = trim($row[product_id]);
$CATALOG[$sku][prod_code] = trim($row[product_code]);
$CATALOG[$sku][prod_name] = trim($row[product_name]);
$CATALOG[$sku][prod_desc] = trim($row[product_description]);
$CATALOG[$sku][prod_price] = trim($row[product_price]);
$CATALOG[$sku][prod_image] = trim($row[product_image]);
}
/*if this is an add operation
add to already existing quantities in shopping cart*/
if (isset($_GET['delete']))
{
unset($_SESSION['cart'][$_GET['delete']]);
//unset($myShopping['order'][$_GET['delete']]);
}
if (isset($_POST['add']))
{
foreach ($_POST['a_qty'] as $k => $v)
{
if($v != 0)
{
$_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v;
}
}
//}
}
/* if this is an update operation
replace the quantities in shopping cart with values entered*/
if($_POST['update'])
{
if(isset($_POST['u_qty']))
{
foreach($_POST['u_qty'] as $k => $v)
{
//if the value is empty, 0 or negative
//dont bother changing the cart
if($v != "" && $v >= 0)
{
$_SESSION['cart'][$k] = $v;
}
}
}
}
if($_POST['process'])
{
$date = date('Y-m-d');
$g_total = $_SESSION['grand_total'];
$custid = $_SESSION['c_id'];
mysql_query("Insert into tbl_order(date_order,customer_id, total_amount) values('$date','$custid','$g_total')") or die(mysql_error());
$ordid = mysql_insert_id();
if(isset($_SESSION['cart']) && is_array($_SESSION['cart']))
{
foreach($_SESSION['cart'] as $k => $v)
{
$prodcode = $CATALOG[$k][prod_code];
$prodname = $CATALOG[$k][prod_name];
$prodprice = $CATALOG[$k][prod_price];
$qty = $v;
$total = $qty * $prodprice;
mysql_query("Insert into tbl_order_details(order_id, product_code, product_name, product_price, quantity, sub_total) values('$ordid','$prodcode','$prodname','$prodprice','$qty','$total')") or die(mysql_error());
}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p><br>
Enter the quantity of the product you wish to order.</p>
<table width="650" border="0">
<tr>
<td width="142">PRODUCT CODE</td>
<td width="187"><div align="center">PRODUCT NAME</div></td>
<td width="121"><div align="center">IMAGE</div></td>
<td width="74"><div align="center">PRICE</div></td>
<td width="114"><div align="center">QUANTITY</div></td>
</tr>
</table>
<table width="655" border="0">
<?php
//print items from the catalog for selection
if(is_array($CATALOG))
{
foreach($CATALOG as $k => $v)
{ ?>
<tr><td width="176"><b><?php echo $v[prod_code]; ?></b></td>
<td width="173"><b><?php echo $v[prod_name]; ?></b></td>
<td width="125"><a href="<?php echo $v[prod_image]; ?>" onclick="window.open(this.href, 'child', 'height=200, width=250'); return false"><img src="<?php echo $v[prod_image]; ?>" width="74" height="69" title="CLICK TO ENLARGE"></a></td>
<td width="70"><b><?php echo $CATALOG[$k][prod_price]; ?></b></td>
<td width="104"><input size=4 type=text name="a_qty[<?php echo $k; ?>]"></td></tr>
<?php }
} ?>
<tr>
<td colspan="5"><br />
<center><input type="submit" name="add" value="Add items to order" title="Click to Order"></center>
</td>
</tr>
</table>
<br>
<h2>Order List</h2>
<table width="36%" border="0" cellspacing="0">
<?php
//initialize a variable to hold total cost
$total = 0;
$subtotal = 0;
//check the shopping cart
//if it contains values
//look up the SKUs in the $CATALOG array
//get the cost and calculate subtotals and totals
if(is_array($_SESSION['cart']) || isset($_SESSION['cart']))
{
?>
<tr style="font-size:10px"><td colspan="2">Date: <?php echo date('Y-m-d'); ?></td></tr>
<?php //foreach($_SESSION['cart'] as $k => $v)
foreach($_SESSION['cart'] as $k => $v)
{
//only display items that have been selected
//that is, quantities > 0
$qty = $v;
if ($qty > 0)
{
$subtotal = $qty * $CATALOG[$k][prod_price];
$total += $subtotal;
$_SESSION['grand_total'] = $total; ?>
<tr style="font-size:10px"><td colspan="2"><b><?php echo $CATALOG[$k][prod_name]; ?></b></td></tr>
<tr style="font-size:10px"><td>Php<?php echo $CATALOG[$k][prod_price]; ?> x <?php echo $qty; ?></td></tr>
<tr style="font-size:10px"><td>New quantity:</td><td><input size=4 type="text" name="u_qty[<?php echo $k; ?>]" value="<?php //echo $v; ?>"></td></tr>
<tr style="font-size:10px"><td>Sub-total:</td><td>Php<?php echo sprintf("%0.2f", $subtotal); ?></td></tr>
<tr style="font-size:10px">
<td colspan="2"><a href="list_order.php?delete=<?php echo $CATALOG[$k][prod_id]; ?>"><b>REMOVE ORDER</b></a></td>
</tr>
<tr>
<td colspan="2"><hr size="2" color="#3333CC" /></td>
</tr>
<?php } ?>
<?php } ?>
<tr style="font-size:14px">
<td width="68%"><b>Total: </b></td>
<td width="8%"><b>Php <?php echo sprintf("%0.2f", $total) ?></b></td>
</tr>
<tr>
<td><input type="submit" name="update" value="Update" size="20"></td><td><input type="submit" name="process" value="Process" size="20"></td> <!--<input type="submit" name="clear" value="Clear Order">-->
</tr>
</table>
</form>
<?php } ?>
<br /><br />