This is part 2 of the shopping cart. Making the cart.php and list.php.
First off all we will make the list.php. This file shows the items from the database in a list and a link to add the item to the shopping cart.
list.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php include 'db.php'; include 'cart.php'; $id_session = session_id(); $query = "select * from items"; $result = mysql_query($query); echo 'Shop items<br>'; echo '- - - - -<br>'; while($row = mysql_fetch_array($result)) { echo $row["itemName"]; echo ' - '; echo $row["itemDesc"]; echo " <a href='cart.php?action=add_item&id=".$row["itemId"]."'>Add Item</a><br>"; } echo '<br> Items in basket<br>- - - - -<br>'; ShowCart(); ?> |
At the bottom you find the call to ShowCart() function, which is from the cart.php where all the functions of the shopping cart are held.
Now I will show the shopping cart functions which handles the items, it contains functions for adding, updating and showing the cart.
cart.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php include 'db.php'; //database config and connection //the switch gets the variables from the url with the corresponding case switch(isset($_GET['action'])) { //add item from url variable case "add_item": AddProduct($_GET["id"]); ShowCart(); break; //remove item from cart (not implemented yet!) case "remove_item": RemoveItem($_GET["id"]); ShowCart(); break; } //function add product to cart function AddProduct($itemId) { $id_session = session_id(); $result = mysql_query("select count(*) from cart where sessionId = '" . $id_session . "' and itemId = $itemId"); $row = mysql_fetch_row($result); $numRows = $row[0]; if($numRows == 0) { //This item doesn't exist in the users cart, //we will add it with an insert query @mysql_query("insert into cart(sessionId, itemId, qty) values('" . $id_session . "', $itemId, 1)"); header("Location: list.php"); } else { //This item already exists in the users cart, //we will update it instead UpdateProduct($itemId); } } //function update product +1 if product is already in cart function UpdateProduct($itemId, $qty) { $id_session = session_id(); @mysql_query("update cart set qty = qty + 1 where sessionId = '" . $id_session . "' and itemId = $itemId"); header("Location: list.php"); } //select current user shopping cart and get total function ShowCart(){ $id_session = session_id(); $totalCost = 0; $sql = "SELECT * FROM cart INNER JOIN items ON cart.itemId = items.itemId WHERE cart.sessionId = '" . $id_session . "'"; //open the table and print a header row if ($result=mysql_query($sql)) { while ($row=mysql_fetch_array($result)) { echo "".$row['itemName']." "; echo ' - '; echo "".$row['itemDesc']." "; echo "".$row['itemPrice']."<br>"; //multiplay quantity and itemprice to get total of 1 product all together $totalCost += $row["qty"] * $row["itemPrice"]; } } else { echo mysql_error(); } echo "Total: "; //format of the totalcost 2 decimals and overall total echo number_format($totalCost, 2, ".", ","); } ?> |
I hope you understand most of it. If not, please post a comment or send me an email. Comments are welcome as it also helps other users to understand it better.
The source can be downloaded here.






29 Responses
It is no good.. To much strings attached, can do shorter..
D6ThyP http://www.cRk2bdPqQls602mIa4bgo.com
I have adapted your script to fit in with my project and would like to thank you for creating such a clear tutorial for beginners like me to follow.
One question: Have you sorted out the remove_item function as yet. I keep trying to do it myself but am getting nowhere. Told you I was a beginner!
Thanks again
Rob
Great article. Waiting for more.
Thank you for one more great weblog.Wherever else could I get this sort of details composed in this kind of an incite total way? I’ve a project that I’m just now operating on, and i’m certain this can aid me a great deal..and I have been searching for such info because from handful of days….Thanks!!!!!
With all the doggone snow we have gotten recently I am stuck indoors, fortunately there is the internet, thanks for giving me something to do.
be happy and love. kiss
This was definitely an fascinating subject, I’m particularly fortunate to have the ability to come to your weblog and I’ll bookmark this internet page in order that I might come back 1 other time.
Comfortably, the post is during truthfulness a hottest on this subject well known subject matter. I agree with ones conclusions and often will desperately look ahead to your updates . Saying thanks a lot will not just be sufficient, for ones wonderful ability in your producing. I will immediately grab ones own feed to stay knowledgeable from any sort of update versions. Amazing get the done and much success with yourbusiness results!
Comfortably, the blog post is during truthfulness a hottest on this subject well known subject matter. I agree with ones conclusions and often will desperately look ahead to your updates . Saying thanks a lot will not just be sufficient, for ones wonderful ability in your producing. I will immediately grab ones own feed to stay knowledgeable from any sort of update versions. Amazing get the job done and much success with yourbusiness results!
Excellent post. I was checking continuously this blog and I’m impressed! Extremely useful information specifically the last part
I care for such info much. I was seeking this particular information for a very long time. Thank you and best of luck.
Brilliant blog posting. I found your post very interesting, I think you are a brilliant writer. I added your blog to my bookmarks and will return in the future.
It’s often a superb concept to go into the store that you are purchasing the paints from and ask for a demonstration and try not to purchase paints on line as you will in no way know what you could wind up with. When acquiring paints in-store try to get them from a comparable batch so that you know what you’ll be acquiring.
mr camel toe Awesome Post. I add this Blog to my bookmarks.
I would like to thnkx for the efforts you’ve put in writing this blog. I am hoping the same high-grade site post from you in the upcoming also. Actually your creative writing abilities has inspired me to get my own blog now. Actually the blogging is spreading its wings rapidly. Your write up is a great example of it.
Let me begin by saying nice post. Im not positive if it has been talked about, but when making use of Chrome I can by no means get the whole website to load with out refreshing a number of times. Could just be my laptop or computer. Thanks.
^^^…
Acquaintance: a person whom we know well enough to borrow from, but not well enough to lend to….
Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it
*The next time I read a blog, I hope that it doesnt disappoint me as considerably as this 1. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear can be a bunch of whining about something that you could fix if you werent too busy looking for attention.
Please let me know if you’re looking for a writer for your blog. You have some really great articles and I think I would be a good asset. If you ever want to take some of the load off, I’d love to write some articles for your blog in exchange for a link back to mine. Please shoot me an email if interested. Regards!
Hello very cool site!! Guy .. Beautiful .. Amazing .. I’ll bookmark your site and take the feeds additionally…I’m glad to find a lot of helpful information here in the publish, we want work out more strategies on this regard, thank you for sharing. . . . . .
Spot on with this write-up, I really assume this website needs way more consideration. I’ll probably be once more to read way more, thanks for that info.
very nice put up, i definitely love this website, carry on it
Good post and right to the point. I am not sure if this is truly the best place to ask but do you people have any thoughts on where to hire some professional writers? Thank you
I’ve been browsing on-line more than 3 hours nowadays, but I by no means discovered any attention-grabbing article like yours. It’s lovely price sufficient for me. In my opinion, if all website owners and bloggers made good content as you probably did, the internet shall be a lot more helpful than ever before.
Hey there! Do you use Twitter? I’d like to follow you if that would be ok. I’m absolutely enjoying your blog and look forward to new updates.
I just want to say I am just newbie to blogs and truly savored your website. Very likely I’m planning to bookmark your site . You actually come with superb posts. Thanks a lot for sharing with us your webpage.
For anyone just copying the code above…
Line 36 needs to be changed to the following:
UpdateProduct($itemId, $qty);
If not your web browser will throw up an error when you want to add 2+ quantity of an item.
It will be my own enjoyment to collect a lot more information from a site and come around offer other folks things i have benefited from you. I thank you for usual extraordinary effort clef-litsud.com Manny Pacquiao vs Bradley boxing fight or Simple PHP MySQL Shopping cart – PART 2 | geforcy dot net.