Question

In: Computer Science

could anyone make a php code for a cart page for an e commerce website?? just...

could anyone make a php code for a cart page for an e commerce website?? just the cart page not the whole website

Solutions

Expert Solution

I have retrieved information like name, code, price, and photos from the database. The resultant information is in an array format.

I have iterated this resultant array to form the product gallery. Every product in the gallery will have an add-to-cart option.

I have used the PHP shopping cart session to store and manage the items in the cart.

Once the session expires, the cart items get cleared. This code has an option to clear the entire cart or to remove any particular item from the cart.

i have used the following file structures. the list given below has the file names and their responsibility.

  • dbcontroller.php – a generic database layer to help with DAO functions. It also manages the database connection.
  • index.php – to display the product gallery for the shopping cart.
  • style.css – to showcase products for the shopping cart. The styles are minimal and cross-browser compatible.
  • tblproduct.sql – contains SQL script with the product table structure and the data.\
  • product-images – a folder that contains the product images. I have used these images to show the product gallery.

Now, i will write down the codes for specific functionalities of the cart.

1) Creating a Product Gallery for Shopping Cart

<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) { 
        foreach($product_array as $key=>$value){
?>
        <div class="product-item">
                <form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
                <div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
                <div class="product-tile-footer">
                <div class="product-title"><?php echo $product_array[$key]["name"]; ?></div>
                <div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
                <div class="cart-action"><input type="text" class="product-quantity" name="quantity" value="1" size="2" /><input type="submit" value="Add to Cart" class="btnAddAction" /></div>
                </div>
                </form>
        </div>
<?php
        }
}
?>

2) Adding Products to Shopping Cart

case "add":
        if(!empty($_POST["quantity"])) {
                $productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
                $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"], 'image'=>$productByCode[0]["image"]));
                
                if(!empty($_SESSION["cart_item"])) {
                        if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
                                foreach($_SESSION["cart_item"] as $k => $v) {
                                                if($productByCode[0]["code"] == $k) {
                                                        if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                                                $_SESSION["cart_item"][$k]["quantity"] = 0;
                                                        }
                                                        $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                                                }
                                }
                        } else {
                                $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                        }
                } else {
                        $_SESSION["cart_item"] = $itemArray;
                }
        }
        break;

3) List Cart Items from the PHP Session

<div id="shopping-cart">
<div class="txt-heading">Shopping Cart</div>

<a id="btnEmpty" href="index.php?action=empty">Empty Cart</a>
<?php
if(isset($_SESSION["cart_item"])){
    $total_quantity = 0;
    $total_price = 0;
?>   
<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;">Name</th>
<th style="text-align:left;">Code</th>
<th style="text-align:right;" width="5%">Quantity</th>
<th style="text-align:right;" width="10%">Unit Price</th>
<th style="text-align:right;" width="10%">Price</th>
<th style="text-align:center;" width="5%">Remove</th>
</tr>     
<?php                
    foreach ($_SESSION["cart_item"] as $item){
        $item_price = $item["quantity"]*$item["price"];
                ?>
                                <tr>
                                <td><img src="<?php echo $item["image"]; ?>" class="cart-item-image" /><?php echo $item["name"]; ?></td>
                                <td><?php echo $item["code"]; ?></td>
                                <td style="text-align:right;"><?php echo $item["quantity"]; ?></td>
                                <td  style="text-align:right;"><?php echo "$ ".$item["price"]; ?></td>
                                <td  style="text-align:right;"><?php echo "$ ". number_format($item_price,2); ?></td>
                                <td style="text-align:center;"><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction"><img src="icon-delete.png"  /></a></td>
                                </tr>
                                <?php
                                $total_quantity += $item["quantity"];
                                $total_price += ($item["price"]*$item["quantity"]);
                }
                ?>

<tr>
<td colspan="2" align="right">Total:</td>
<td align="right"><?php echo $total_quantity; ?></td>
<td align="right" colspan="2"><strong><?php echo "$ ".number_format($total_price, 2); ?></strong></td>
<td></td>
</tr>
</tbody>
</table>          
  <?php
} else {
?>
<div class="no-records">Your Cart is Empty</div>
<?php 
}
?>
</div>

4) Removing or Clearing Cart Item

case "remove":
        if(!empty($_SESSION["cart_item"])) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($_GET["code"] == $k)
                                unset($_SESSION["cart_item"][$k]);                              
                        if(empty($_SESSION["cart_item"]))
                                unset($_SESSION["cart_item"]);
                }
        }
        break;
case "empty":
        unset($_SESSION["cart_item"]);
        break;

5) Database Product Table for Shopping Cart

--
-- Table structure for table `tblproduct`
--

CREATE TABLE `tblproduct` (
  `id` int(8) NOT NULL,
  `name` varchar(255) NOT NULL,
  `code` varchar(255) NOT NULL,
  `image` text NOT NULL,
  `price` double(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tblproduct`
--

INSERT INTO `tblproduct` (`id`, `name`, `code`, `image`, `price`) VALUES
(1, 'FinePix Pro2 3D Camera', '3DcAM01', 'product-images/camera.jpg', 1500.00),
(2, 'EXP Portable Hard Drive', 'USB02', 'product-images/external-hard-drive.jpg', 800.00),
(3, 'Luxury Ultra thin Wrist Watch', 'wristWear03', 'product-images/watch.jpg', 300.00),
(4, 'XP 1155 Intel Core Laptop', 'LPN45', 'product-images/laptop.jpg', 800.00);

--
-- Indexes for table `tblproduct`
--
ALTER TABLE `tblproduct`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `product_code` (`code`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tblproduct`
--
ALTER TABLE `tblproduct`
  MODIFY `id` int(8) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;

6) Simple PHP Shopping Cart Output

i hope you understood the concept well. if you did, please click on the 'like' button.

Also if you have any doubt regarding any concept taught above, do mention in the comments. i will try to answer it as soon as possible.


Related Solutions

e-commerce Hi I have project in e-commerce I  will create a website that sells shirts to people,...
e-commerce Hi I have project in e-commerce I  will create a website that sells shirts to people, holidays, events, or school students Create your own e-commerce business in Saudi Arabia (website or mobile application), you need to explain the process that you will follow in building your e-commerce presence. Before you begin to build a website or app of your own, there are some important questions you will need to think about and answer. The answers to these questions will drive...
What type of security on e-commerce website? To whom or to what? What are the Potential...
What type of security on e-commerce website? To whom or to what? What are the Potential threats to your website?
Trade and trade is a new page, an e-commerce platform on facebook , deals in the...
Trade and trade is a new page, an e-commerce platform on facebook , deals in the business of online shopping, its similar to amazon. Facebook policy of getting a VERIFIED BADGE or BLUE TICK is best for any new business but it is very difficult to attain. You have to make and explain Facebook strategies of how this page can get verificatio Please give information of around 600-800 words! comprehensively but quick!!!
2) Select an e-commerce company & based on the information you found on their website, briefly...
2) Select an e-commerce company & based on the information you found on their website, briefly 0. Describe their business model 15 pts. 1. Identify their customer value proposition 15 pts. 2. Identify its revenue model 15 pts. 3. Identify their main competitors 15 pts. 4. Identify their market strategy 20 pts
e-commerce what could be an online business which could be started or already running but notnin...
e-commerce what could be an online business which could be started or already running but notnin everypart of the world( not that famous)? include business model and cost included. consider and examine business opportunities online thanks
There are eight different important factors in e-commerce website design, which are: Functionality Informational Ease of...
There are eight different important factors in e-commerce website design, which are: Functionality Informational Ease of Use Redundant Navigation Ease of Purchase Multi-Browser functionality Simple graphics Legible text. Topic: Go to an e-commerce website of your choosing and evaluate its effectiveness according to the basic criteria mentioned above.
There are eight different important factors in e-commerce website design, which are: Functionality Informational Ease of...
There are eight different important factors in e-commerce website design, which are: Functionality Informational Ease of Use Redundant Navigation Ease of Purshase Multi-Browser functionality Simple graphics Legible text. Go to an e-commerce website of your choosing and evaluate it's effectiveness according to the basic criteria mentioned above.
A chocolate e-commerce website selling its goods and delivering it to its customers . Ofcourse infront...
A chocolate e-commerce website selling its goods and delivering it to its customers . Ofcourse infront of many e-commerce sites there are many threats and many secure security concerns should take place . As for the case of the chocolate e-commerce site : 1) What kind of threats can the site faces? ( atleast 4 threats) 2) Which technical and non technical precautions can the site developers do to respond to these threats ? ( 3 precautions each ie, technical...
Identify an e-commerce website whose interface has some design challenges. Evaluate the three parts of the...
Identify an e-commerce website whose interface has some design challenges. Evaluate the three parts of the user interface (navigation, input, and output) using the six principles of interface design (layout, content awareness, aesthetics, user experience, consistency, and minimal user effort). To which principles does the interface adhere successfully? Why? In which areas does it fall short? Explain. Choose one problem you found with the interface. Which step of the interface design process should have prevented the problem? Explain.
5. Does it make good strategic sense for Amazon to be a competitor in the e-commerce,...
5. Does it make good strategic sense for Amazon to be a competitor in the e-commerce, cloud-based computing services, and personal media device industries? Which of its three principal product lines—e-commerce, cloud computing services, or personal media players—do you think is most important to Amazon’s future growth and profitability? Why? Should any of the product lines be discontinued?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT