i am sending my full code:-
this is so.php for shopping cart:-
Expand|Select|Wrap|Line Numbers
- <html> <center><table width="100%" height="300"> <tr width="100%" height="100%"> <td width="70%" height="70%"> <?php
- session_start();
- $server="localhost";
- $user="root";
- $password="";
- $db="online sabzi mandi";
- mysql_connect($server,$user,$password) or die("sorry can't connect to mysql");
- mysql_select_db($db) or die("sorry can't connect to database");
- if(isset($_GET['page'])) {
- $pages=array("pro","cart","11");
- if (in_array($_GET['page'],$pages)){
- $_page=$_GET['page'];
- $quantity='$_POST[quantity]';
- }else{
- $_page="pro";
- }
- }
- else{
- $_page="pro";
- }
- ?> <?php require($_page.".php" ); ?> </td> <td width="30%" height="70%"> <center><h1>cart</h1></center> <table> <tr> <td><b>name</b></td> <td><b>item price</b></td> </tr> <?php
- $sql="select * from products where id IN(";
- foreach($_SESSION['cart'] as $id => $value ){
- $sql.=$id.",";
- }
- $sql=substr($sql, 0, -1).")";
- $query=mysql_query($sql);
- //$quantity='$_POST[quantity]';
- $quantity = isset($_POST["quantity"]) ? $_POST["quantity"] : 'No data from $_POST';
- $totalprice=0;
- if(!empty($query)){
- while ($row=mysql_fetch_array($query)) {
- $subtotal=$_SESSION['cart'][$row['id']][$quantity]*$row['price'];
- $totalprice+=$subtotal;
- ?> <tr> <td><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id']][$quantity]?></td> <td><?php echo $_SESSION['cart'][$row['id']][$quantity]*$row['price'] ?></td> </tr> <?php
- }
- }else{
- echo "You need to add some items.your cart is empty";
- echo "<img src='http://bytes.com/images/empty-cart-dark.png' width='150' height='140'></br>";
- }
- ?> <b>Total price:</b> <?php echo $totalprice ?><br> <a href="http://bytes.com/so.php?page=cart">Go to Cart</a> </td> </tr> </table> </center> </html>
this is pro.php for product:-
Expand|Select|Wrap|Line Numbers
- <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="http://bytes.com/css/style.css"> <script src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script> <script src="http://bytes.com/js/incrementing.js"></script> </head> <?php
- if(isset($_GET['action']) && $_GET['action']=="add" ) {
- $id=intval($_GET['id']);
- $quantity='$_POST[quantity]';
- if(isset($_SESSION['cart'][$id])){
- $_SESSION['cart'][$id][$quantity];
- }else{
- $sql_s="select * from products where id={$id}";
- $query_s=mysql_query($sql_s);
- if(mysql_num_rows($query_s)!=0){
- $row_s=mysql_fetch_array($query_s);
- $_SESSION['cart'][$row_s['id']]=array(
- "quantity" => $_POST['quantity'] ,
- "price" =>$row_s['price']
- );
- }else{
- $message="this product id is invalid";
- }
- }
- }
- ?> <table width="100%"> <tr width="100%" height="100%"> <td width="70%" height="70%"> <?php
- if(isset($message)){
- echo "$message";
- }
- //echo print_r($_SESSION['cart']);
- ?> <table width="100%"> <tr> <td>name</td> <td>price</td> <td>Quantity in Kg</td> </tr> <?php
- $sql="select * from products";
- $query=mysql_query($sql);
- while($row=mysql_fetch_array($query)){
- ?> <tr> <td><?php echo $row['name'] ?></td> <td><?php echo $row['price'] ?></td> <form method="post" action="so.php?page=pro&action=add&id=<?php echo $row['id'] ?>"> <td class="numbers-row"> <input type="text" name="quantity" id="quantity" value="1"><br> </td> <td class="buttons"> <input type="submit" value="Add to cart" id="submit"> </td> </form> </tr> <?php } ?> </table> </td> </tr> </table>
this is cart.php for cart:-
Expand|Select|Wrap|Line Numbers
- <?php
- if(isset($_POST['submit'])) {
- foreach($_POST['quantity'] as $key => $value) {
- if($value==0){
- unset($_SESSION['cart'][$key]);
- }else{
- $_SESSION['cart'][$key]['quantity']=$value;
- }
- }
- }
- ?> <h1>view cart</h1> <a href="http://bytes.com/shopping_cart.php?page=products">Go back to product page</a> <form method="post" action="http://bytes.com/shopping_cart.php?page=cart"> <table width="100%" height="70%"> <tr> <th>name</th> <th>quantity in kg</th> <th>price</th> <th>items price</th> </tr> <?php
- $sql="select * from products where id IN(";
- foreach($_SESSION['cart'] as $id => $value){
- $sql.=$id.",";
- }
- $sql=substr($sql, 0, -1).")";
- $query=mysql_query($sql);
- $totalprice=0;
- if(!empty($query)){
- while ($row=mysql_fetch_array($query)) {
- $subtotal=$_SESSION['cart'][$row['id']]['quantity']*$row['price'];
- $totalprice+=$subtotal;
- ?> <tr> <td> <?php echo $row['name'] ?></td> <td> <select name="quantity[<?php echo $row['id'] ?>]" value="<?php echo $_SESSION['cart'][$row['id']]['quantity']?>"/> <option>0</option> <option>0.5</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select></td> <td> <?php echo $row['price'] ?></td> <td> <?php echo $_SESSION['cart'][$row['id']]['quantity']*$row['price'] ?></td> </tr> <?php
- }
- }else{
- echo "<h3><i>You need to add some items.your cart is empty</i></h3>";
- }
- ?> <tr> <td>total price <?php echo $totalprice ?></td> </tr> </table> <button type="submit" name="submit">update cart</button>
- ******<a href="http://bytes.com/2.html" target="_blank">Place Order</a> <p>to remove an item set its quantity to 0</p> </form> <?php
- ?>