this is saved as index.php
<html> <head> <title>Catalog</title> <link rel="stylesheet" href="mm_travel2.css" type="text/css" /> </head> <body bgcolor="#C0DFFD"> <tr> <td width="40"> </td> <td colspan="2" valign="top"> <br /> <br /> <table border="0" cellspacing="0" cellpadding="2" width="610"> <h1 align="left">Super Simple Shopping Cart<h1> <tr> <td><img src="sherlock-holmes-set-of-5-books.jpeg" alt="Sherlock Holmes" width="200" height="150" border="3" /></td> <td> </td> <td><img src="transcend-jm1066ksn-2g-400x400-imad57wzgufgxuhy.jpeg" alt="Jetram RAM" width="200" height="150" border="3" /></td> <td> </td> <td><img src="hp.jpeg" alt="HP Pen Drive" width="200" height="150" border="3" /></td> </tr> <tr> <td><a href="order.php?add=Sherlock Holmes(Set of 5 books)&price=336.00&qty=1">Sherlock Holmes(Set of 5 books)</a><br /> Price: $336.00</td> <td> </td> <td><a href="order.php?add=JetRam Laptop 2GB RAM&price=839.00&qty=1">JetRam Laptop 2GB RAM</a><br /> Price: $839.00</td> <td> </td> <td><a href="order.php?add=HP 8GB Pen Drive&price=363.00&qty=1">HP 8GB Pen Drive</a><br /> Price: $363.00</td> </tr> <tr> <td colspan="7"> </td> </tr> <tr> <td><img src="c-the-com.jpeg" alt="C++ Book" width="200" height="150" border="3" /></td> <td> </td> <td><img src="samsung.jpeg" alt="Memory Card" width="200" height="150" border="3" /></td> <td> </td> <td><img src="panasonic-sr-wa-10-sr-wa-10-400x400-imadbgg7ebyz6wyf.jpeg" alt="Electric Cooker" width="200" height="150" border="3" /></td> </tr> <tr> <td><a href="order.php?add=C++ Complete Reference Book&price=474.00&qty=1">C++ Complete Reference Book</a><br /> Price: $474.00</td> <td> </td> <td><a href="order.php?add=Samsung 4GB Memory Card&price=299.00&qty=1">Samsung 4GB Memory Card</a><br /> Price: $299.00</td> <td> </td> <td><a href="order.php?add=Panasonic Electric Cooker&price=1000.00&qty=1">Panasonic Electric Cooker</a><br /> Price: $1,000.00</td> </tr> <tr> <td colspan="7"> </td> </tr> </table> </td> <td width="100%"> </td> </tr> <tr> <td> </td> <td><a href="order.php">View Cart</a></td> <td> </td> <td> </td> </tr> </table> </body> </html>
this is saved as order.php
<?php session_start(); if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); } if (isset($_GET['itemID']) && isset($_GET['price']) && isset($_GET['qty'])){ $ITEM = array( 'name' => $_GET['itemID'], 'price' => $_GET['price'], 'qty' => $_GET['qty'] ); $_SESSION['SHOPPING_CART'][] = $ITEM; header('Location: ' . $_SERVER['PHP_SELF']); } else if (isset($_GET['remove'])){ unset($_SESSION['SHOPPING_CART'][$_GET['remove']]); header('Location: ' . $_SERVER['PHP_SELF']); } else if (isset($_GET['empty'])){ session_destroy(); header('Location: ' . $_SERVER['PHP_SELF']); } else if (isset($_POST['update'])) { foreach ($_POST['items_qty'] as $itemID => $qty) { if ($qty == 0) { unset($_SESSION['SHOPPING_CART'][$itemID]); } else if($qty >= 1) { $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty; } else if($qty < 0) { echo "*....Quantity must be 1 or more"; } } //header('Location: ' . $_SERVER['PHP_SELF']); } ?> <html> <head> <title>Online Order Form</title> </head> <body> <h1>Shopping Cart</h1> <form action="" method="post" name="shoppingcart"> <table width="500" border="1"> <tr> <th> </th> <th>Item Name</th> <th>Unit Price</th> <th>Qty</th> <th>Cost</th> </tr> <?php $GrandTotal=0; foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) { $GrandTotal = $GrandTotal + ($item['qty']*$item['price']); ?> <tr id="item<?php echo $itemNumber; ?>"> <td><a href="?remove=<?php echo $itemNumber; ?>">Remove</a></td> <td><?php echo $item['name']; ?></td> <td><?php echo $item['price']; ?></td> <td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td> <td><?php echo $item['qty'] * $item['price']; ?></td> </tr> <?php } ?> </table> <p> <table> <tr id="item<?php echo $itemNumber; ?>"> <td><p><strong>Grand Total: $<?php echo $GrandTotal;?></strong></p><td> </tr> </table> <label> <input type="submit" name="update" id="update" value="Update Cart" /> </label> </p> </form> <p><a href="index.php">Continue Shopping</a> - <a href="?empty">Empty Cart</a> </p> </div> </body> </html> help me as soon as possible Thanks in advance for the help
Hi,
Before adding into the array you should check if the item is already present?
You can iterate the items through array and then add the items to the array if it is not present.
Thanks
thanks for providing me the solution... But will you please explain it with the help of the code because I am Beginner to php and this is my first try to any php assignment...
Thanks in advance
Ads