Hi! 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.
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!
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.
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.
Now let us take a quick look at how selections are used in html markup. It is easier than you might think:
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:
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! 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!





Comments on: "Building A Shopping Cart – Part 2" (2)
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.
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!