Shopping Cart In PHP – Learn PHP Programming

A shopping cart in php will be a good example for learning. When I started using PHP, I began like most other people; the basics. I then promised to show you how to create a simple shopping cart. I have to warn you beforehand, this is not really a complex version. I figured it is smart to start with a simpler version then add more functionality to it as we proceed. So, let us get to it. I am going to do this in reverse, that is, show you snapshots of the cart then dissect the code. So, here we go!

shopping cart in php

Assuming you were looking for oil, spark plugs and tires for your good old truck, you would visit Crazy Auto Store and add them to the cart. As shown above, you enter the quantities into the input area. Simple right? Assuming you have done that, and hit ‘Submit Your Order’ button: Here is what you will see:

shopping cart in php tutorial

Yeah, when I said it was going to be simple, I meant it. Now let us take a look at both the HTML and PHP that made the above possible!

Shopping Cart In PHP : HTML FILE (store.html)


< !DOCTYPE html>


That is the html file (store.html). As you can see, I linked an external css file for presentation. Now that we have got that out of the way, we can look back at the first snapshot and see where it came from. Let us get the PHP file started.

PHP FILE (store.php)
< !DOCTYPE html>

Crazy Auto Store

Below is your order summary:

< ?php //declare the variables here $oilqty = 0; $sparksqty = 0; $tiresqty = 0; $total = 0; //Now let us get the values from the form if(isset($_POST['oil'])){ $oilqty = &$_POST['oil']; } if(isset($_POST['sparks'])){ $sparksqty = &$_POST['sparks']; } if(isset($_POST['tires'])){ $tiresqty = &$_POST['tires']; } $total = $oilqty + $sparksqty + $tiresqty; echo 'You ordered on ‘. date(‘H:i, jS F Y’). ‘:’;
echo ‘
echo ‘ ‘.$oilqty. ‘ Oil Containers
echo ‘ ‘.$sparksqty. ‘ Spark Plugs ‘;
echo ‘ ‘.$tiresqty. ‘ New Tires
echo ‘ ‘.$total. ‘ Total Items ‘;

Copyright 2013 © Shopping Cart In PHP


Again, I used my own css here while creating this shopping cart in php. This should do it. That is all you need to accomplish what I showed you in the snapshots. Easy right? Just a brief explanation for the php file:

You might have noticed a couple of things: isset() and $_POST[]. I used the isset() method to check whether the values of (oil,sparks, tires) are set in the $_POST[] array. So I have already answered the ‘what is the $_POST[]’ thingy. When you click submit after filling up the form, the data you entered is stored in that $_POST array, that way, you can access them later and do some calculations or magic!

Next time, I will be building on top of what I did today, to add more functionality to our shopping cart in php! Think about shipping, discounts and more stuff! I hope to see you around and if you have any questions, please let me know and I will be glad to talk to you. Take care!

Please share this post if you liked it and let me know if you have any comments because I would love to hear from you! Please subscribe to more great posts!

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

4 Comments on “Shopping Cart In PHP – Learn PHP Programming

  1. brunoais Reply

    January 26, 2013 at 9:59

    Place the inputs inside the labels.
    If you really require them outside, then use it properly.
    The label@for requires an id, not a name. Use an id of the input element it refers to.

    The tag button does not have a @type. Use @type=”submit” (it was already smart of you to use a instead of an , nice ;))

    //declare the variables here
    13 $oilqty = null;
    14 $sparksqty = null;
    15 $tiresqty = null;
    16 $total = null;”

    no! 0! (an integer)

    BTW, use:
    $oilqty = &$_POST[‘oil’];
    instead of:
    $oilqty = $_POST[‘oil’];

    Just… don’t make copys of variables without need.

    • joylaxpower Reply

      January 26, 2013 at 6:24

      Great points thanks. About placing inputs outside the labels, I have not seen that kind of thing before. Perhaps I should give it a reading. Either way, much appreciated!!

      I just noticed that issue about the button! Let me fix them ASAP. I have not used copying in PHP before and from now on, I know what needs to be done! Thanks

      • brunoais Reply

        January 28, 2013 at 9:07

        “About placing inputs outside the labels, I have not seen that kind of thing before. Perhaps I should give it a reading. Either way, much appreciated!!”
        You just did. But you didn’t do it properly.

        BTW, if you are doing it in HTML5, then do it better to suit the HTML5 itself!

  2. […] the above image for a closer view of the entire code. This code contains some pieces from the first Shopping Cart Part I and then the calculations and constants definition and […]

Leave a Reply

Your email address will not be published. Required fields are marked *