Shopping Cart Using PHP – Learning PHP Part II

A shopping cart using php is a good place to learn how php works. I had promised to improve our simple shopping cart by adding some functionality to it. Today therefore, I have added a drop-down list to our web store so that customers can select an option describing where they found our store. It could be through television ads, phone directory, word of mouth or other sources. So, without much ado, am going to show you how our page will look like then share the php and html code.

shopping cart using php

As you can see above, you get a list of different sources. Now, in the next snapshot, I am going to show you another feature that I added to dynamically display a table which you can use to determine your shipping cost. I used a for loop to generate it. Cool!

shopping cart using php

You might have noticed something new in the above snapshot: the total costs and taxes inclusion. There is also the table shown by the arrow. Now, if you select an option from the drop-down, it will be displayed just below the total amount! Now let us take a look at the code.

shopping cart using php

Simply click on 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 usage.

Shopping cart using php – Code

Now let us take a quick look at how selections are used in html markup. It is easier than you might think:

shopping cart using php

What I did here was to simply add a <div> tag just below what I had in my first post. I added to the same file! Again, please click on the image to see the code clearly. Now let us take a look at the switch statement that utilizes the values returned by the above selection:

shopping cart using php

As you can see, using a switch statement is much better than using a lot of if else statements. I went a head and added the code to dynamically generate the table that shows the distance ::: shipping information. I used a for loop! Please click on the image to see a clear version of the image. I apologize for that!

That should do it for today for our shopping cart in php! I know it is not that complicated but I thought it was good for me to start small and then move on to intermediate levels. I really liked reading through the PHP documentations and I think everyone trying to learn how to do scripting using PHP should give it a substantial amount of time. Thanks for visiting and if you have any questions or see any errors, please don’t hesitate to drop me a line using the comments! I will really like to connect with you!

Take care and see you soon!

Please share this post if you liked it and subscribe for more posts

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

2 Comments on “Shopping Cart Using PHP – Learning PHP Part II

  1. brunoais Reply

    January 30, 2013 at 11:37

    Why did you stop writing the code here? Now I need to open all images in a new tab!
    If I want to try the code, there’s no way I can copy and paste it.

    • joylaxpower Reply

      January 30, 2013 at 6:38

      I apologize for that. You see, I did that because I ran out of time to type on WordPress. All the upcoming code will be written on my blog – I promise, unless otherwise stated.

      Thanks for your visit and see you soon!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.