PHP Shopping Cart for Beginners
php shopping cart How to add item and display for beginners--codenair |
Hi Friends Today We are going to learn how to create simple shopping cart in php..this is basic session based shopping cart for beginners..
in this this lesson we learn how add items into session and display them..
learn more about PHP SESSION Follow The below Step:
1. Step 1:
Create 2 Indexed array named 1. product and 2. price
use the below codes to create and display products with price from array..
<?php session_start(); $product=array('Nokia 1020','ASUS Vivobook','Sony XA Ultra','HP Power Gammer'); $price=array('760','580','340','780'); ?> <h3>Products</h3> <table border="1" cellspacing="0"> <tr> <th>Product Name</th> <th>Price</th> <th>Action</th> </tr> <?php for($i=0;$i<count($product);$i++){?> <tr> <td><?php echo $product[$i];?></td> <td>$<?php echo $price[$i];?></td> <td><a href="?add=<?php echo $i;?>">Add Cart</a></td> </tr> <?php }?> </table>
2. Step 2:
when a user will click add cart link we will store all added item of current user into session variable..
look the below codes..
<?php //adding product if(isset($_GET['add'])){ $id=$_GET['add']; $qty=$_SESSION['qty'][$id]+1; $_SESSION['price'][$id]=$qty*$price[$id]; $_SESSION['qty'][$id]=$qty; $_SESSION['cart'][$id]=$id; header('location:index.php'); } ?>
3. Step 3:
in This step we will retrieve data/added items from session..
<?php if(!empty($_SESSION['cart'])){?> <h2>Your Cart</h2> <table border="1" cellspacing="0"> <tr> <th>Product</th> <th>Quantity</th> <th>Price</th> </tr> <?php $total=0; foreach($_SESSION['cart']as $row){?> <tr> <td><?php echo $product[$_SESSION['cart'][$row]];?></td> <td><?php echo $_SESSION['qty'][$row];?></td> <td>$<?php echo $_SESSION['price'][$row];?></td> </tr> <?php }?> </table> <?php }else{ echo "Your Cart is Empty"; } ?>
PHP Shopping Cart Full Codes: index.php
<?php session_start(); $product=array('Nokia 1020','ASUS Vivobook','Sony XA Ultra','HP Power Gammer'); $price=array('760','580','340','780'); ?> <?php //adding product if(isset($_GET['add'])){ $id=$_GET['add']; $qty=$_SESSION['qty'][$id]+1; $_SESSION['price'][$id]=$qty*$price[$id]; $_SESSION['qty'][$id]=$qty; $_SESSION['cart'][$id]=$id; header('location:index.php'); } ?> <h3>Products</h3> <table border="1" cellspacing="0"> <tr> <th>Product Name</th> <th>Price</th> <th>Action</th> </tr> <?php for($i=0;$i<count($product);$i++){?> <tr> <td><?php echo $product[$i];?></td> <td>$<?php echo $price[$i];?></td> <td><a href="?add=<?php echo $i;?>">Add Cart</a></td> </tr> <?php }?> </table> <?php if(!empty($_SESSION['cart'])){?> <h2>Your Cart</h2> <table border="1" cellspacing="0"> <tr> <th>Product</th> <th>Quantity</th> <th>Price</th> </tr> <?php $total=0; foreach($_SESSION['cart']as $row){?> <tr> <td><?php echo $product[$_SESSION['cart'][$row]];?></td> <td><?php echo $_SESSION['qty'][$row];?></td> <td>$<?php echo $_SESSION['price'][$row];?></td> </tr> <?php }?> </table> <?php }else{ echo "Your Cart is Empty"; } ?>
That's it Friend How to add items and display in php session..
Keep visiting Thank You..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.