Create PHP Mysql Shopping Cart using COOKIES

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends this is one more video tutorial on make shopping cart by using PHP script but here we will make shopping cart by using PHP cookies we have already make video tutorial on how to make PHP shopping cart by using session and in that tutorial we have stored shopping cart data in session variable so if we have refresh page then shopping cart will not be lost and shopping cart data still display on webpage so here also we have make shopping cart and shopping cart data has been stored under cookies variable so if page has been refresh accidentally but shopping cart data will not be lost because it has been stored under cookies variable this cookies data has been stored at client-side in browser and it will be lost on its defined time for this year we have make this video tutorial so if anyone want to make shopping cart by using PHP cookies instead of PHP session variable now let's start discuss this topic so this is our test database and in this we have one product table with table column like ID name image price in this table we have already inserted some product data so we will fetch data from this table and display on webpage for an item into shopping cart by using PHP this is our index page and on this page first we want to make database connections so we have write dollar Connect variable is equal to new class object under this we have right my SQL host is equal to localhost database name set to test username set to root and password set to blank after this we want to display product on webpage so here we have right dollar query variable is equal to select star from product table order by ID in ascending order below this we have right dollars statement variable is equal to dollar Connect variable with preparer statement with dollar query variable this make query for execution now we want to execute query so we have rightt dollars statement variable with execute method this method will execute query after this we have right dollar result variable as equal to dollar result variable is equal to dollar statement with fetch all method this will fetch query execution result and store under dollar result variable below this we have right for each loop with dollar result variable as dollar row variable under this loop first we have write HTML form tag with attribute method is equal to post under this form first we want to display product image so here we have right image tag with attribute source is equal to images folder slash dollar row image variable it will display product image on webpage below this we want to display product name so here we have right dollar row name variable it will display particular product name below product image after this we want to display product price so here we have right dollar row price variable it will display product price below product name now we have right input type is equal to text name is equal to quantity and value is equal to 1 under this text box user can enter number of product quantity for add to cart below this we have right in type is equal to hidden name is equal to hidden name value is equal to dollar row name variable here we have store product name under hidden field same way for store product price we have write input type is equal to hidden name is equal to hidden price value is equal to dollar row price variable after this we have write input type is equal to hidden name is equal to hidden ID value is equal to dollar row ID variable here we have store particular product ID value has been stored under hidden field lastly we want to define button for add item into cart so we have write input type is equal to submit name and value is equal to add to cart by click on this button we can add any item into shopping cart friends here we can see that when page has been load into browser then we can see some product on web with Add to Cart button so when we have click on this button particular item has been added into cart for add item into cart we have go to index page and here we have write if statement and under condition we have write ESET function with dollar post add to cart variable if this variable value is set then it will execute if block of code and under this block we have write dollar item array variable is equal to array in first item ID key value has been get from Dollar post hidden ID variable second item name key value has been get from Dollar post hidden name variable third item price key value has been get from Dollar post hidden price variable and fourth item the key value has been get from dollar post quantity variable this way we have store form data into this dollar item array variable after this we have right dollar cart data array variable is equal to dollar item array variable here we have store dollar item array data into this dollar cart data variable below this we have right dollar item data variable is equal to JSON encode function and under this we have right dollar cart data variable this function will convert PHP array to JSON string now we have right setcookie function with three argument in first argument we have right shopping cart which is name of this cookie in second argument we have right dollar item data variable which is data which we have store under this shopping cart variable and third argument we have right time function plus 86,400 into 30 which is equal to one day that means this cookies data will be expired after one day so this way we have stored shopping cart data into this cookie variable by using setcookie function below this we have right header function and under this we have right location set to index dot PHP question Mart success is equal to one so when we have add item into cart then page has been redirect to this page now here we have write if statement and under condition we have write is set function with dollar get success function if this variable value is set then it will execute this block of code under this we have right dollar message variable is equal to bootstrap success alert and under message we have right item add into shopping cart this message will be displayed after item add into cart now we want to display cart data on webpage after item add into cart so here first we have write echo statement with dollar message variable after this we have create one table with five table column like item name quantity price total and action after this we have write if statement and under condition we have write is set function with dollar cookies shopping cart variable if this variable value is set then it will execute if block of code but suppose above condition false then it will execute else block of code and under this block we have write echo statement with HTML table code with text message like no item in cart but suppose above condition true then it will execute if block of code and under this block we have write dollar total variable is equal to 0 after this we have write dollar cookie data variable is equal to strip slashes function and under this we have write dollar cookies shopping cart variable this function will removes backslashes below this we have write dollar cart data variable is equal to JSON decode function with dollar cookie data variable with true this function will convert JSON string to PHP variable now we have write for each loop and under this we have write dollar cart data variable as Dollar keys variable with dollar value use variable under this loop we want to first display item name so we have right dollar values item name variable after this we want to display number of quantity so we have right echo statement with dollar values item quantity variable same way we want to display item price so here we have right echo statement with dollar values item price variable now we want to display total of particular item so we have right echo statement with number format function with dollar values item quantity variable into dollar values item price variable lastly we want to make link for remove particular item from cart so we have right anchor tag with her F is equal to index dot PHP question mark action is equal to delete and ID is equal to dollar values item id variable so this code will make dynamic remove item link for each item which has been added into cart and when we have click on this link then it will redirect page to this dynamic link and item will be removed below this we have right dollar total variable is equal to dollar total variable plus dollar values item quantity variable into dollar values item price variable this code will make total of whole shopping cart lastly we want to display value of this dollar total variable so we have right echo statement with number format function dollar total variable it will display value of whole shopping cart on webpage friends first we have refresh page and now we have add one product into shopping cart so we have click on particular item Add to Cart button after click on button we have received success message like item has been added into cart and we can see our shopping cart details on webpage but know when we have add same item into cart then at that time we want increase particular item quantity into cart instead of whole item add into cart for this we have go to index page and here we have write if statement and under condition we have write dollar cookies shopping cart variable if we have already item added into cart then it will execute if block of and under this block we have write dollar cookie data variable is equal to strips lashes function and under this we have write dollar cookies shopping cart variable this function will removes backslashes below this we have write dollar cart data variable is equal to JSON decode function with dollar cookie data variable with true this function will convert JSON string to PHP variable and store under dollar cart data variable but suppose we have include first item into cart then it will execute else block of code and under this we have right dollar cart data variable is equal to blank array now we want to get list of item ID which we have added into shopping cart so we have right dollar item id list variable is equal to a R a column function under this function we have right dollar cart data variable and item id key name this function will return value of item id key from dollar cart data variable and store under item id list variable below this we have right if statement and under condition we have right in array function and under this function we have right dollar post hidden ID variable and dollar item ID list variable if added item ID into cart has already been added into shopping cart then it will execute if block of code that means we have already added into cart and again at same item into cart then it will execute if block of code so now we want to only increase item quantity in cart not hole item again added into cart for this we have right for each loop with dollar cart data variable as dollar key variable with dollar value variable under this loop we have right if statement and under condition we have right dollar cart data variable with dollar key index with item id key value is equal to dollar post hidden ID variable if this condition true then it will execute if block of code and under this we have right dollar card data variable with dollar key index item quantity key value is plus an equal to dollar post quantity variable this code will add dollar post quantity variable value into particular item quantity value of item so here we have not add new item into cart but we have only changed item quantity when we have add same item into cart but suppose we have new item into cart then that code we have already made so this code we have put under this else block of code now here friends first we have refresh page but cart data has not been lost and we can see shopping cart data here now we have add same item which we have already added into cart so here we have again at same item which we have already added into cart after clicking on Add to Cart button particular item quantity has been changed but not whole item added into cart now we have procede for remove particular item from cart so here when we have click on particular item remove button then page has been redirect to this URL so we have go to index page and here we have right if statement and condition we have right is set function with duller get action variable if this condition true then it will execute if block of code under this block we have right if statement and under condition we have right dollar get action variable value is equal to delete if this condition true then it will execute if block of code after this we have right Dollar cookie data variable is equal to strip slashes function and under this we have right dollar cookies shopping cart variable this function will removes back slashes below this we have right dollar cart data variable is equal to JSON decode function with dollar cookie data variable with true this function will convert JSON string to PHP variable after this we have write for each loop and under this we have write dollar cart data variable as dollar keys variable with dollar values variable under this loop we have write if statement and under condition we have write dollar cart data variable with dollar keys index item ID key is equal to dollar get ID variable if this condition true then it will execute if block of code under this block we have write unset function and under this we have write dollar cart data variable with dollar keys index this function will destroy this keys array now we want update array data in cookie variable so here we have write dollar item data variable is equal to JSON encode function with dollar cart data variable this function will convert update this variable array into JSON string after this we have write setcookie function with three argument like cookie name shopping cart cookie data get from dollar item data variable and cookie expire time this function will update shopping cart cookie data below this we have right header function and under this we have right location set to index dot PHP question Mart remove is equal one page has been redirect to this URL after item removed from cart so here we have right if is set dollar get removed variable if this variable value is set then it will execute if block of code under this block we have write dollar message variable as equal to bootstrap success alert and under message we have write item removed from cart this message will be display after item removed from cart friends here we can see that some item has been added into cart now we want to remove this item from cart so we have click on remove item remove button after click on remove button we have remove item removed from cart message on web page and that item has been removed from cart and under cart we can see remaining item into cart now we want to remove all product from shopping cart in single click so here on index page we have write anchor tag with our F is equal to index dot PHP question Mart action is equal to clear so when we have click on this link page has been redirect to this URL so here we have write if statement and under condition we have write dollar get action variable value is equal to clear if this condition true then it will execute if block of code and under this block we have write setcookie function with cookie name shopping cookie value set to blank and cookie x per a time this code will expire shopping cart cookie variable with blank data so our shopping cart all data will dis Troi and it will be clear so this way we can clear shopping cart item by using PHP cookie variable below this we have right header function with location set to index dot PHP question mark clear all is equal to one so page has been redirect to this URL after clear all item from shopping cart now here we have write if statement and under condition we have write is set function with dollar get clear all variable if this variable value is set then it will execute if block of code under this block we have write dollar message variable is equal to bootstrap success alert code and under message we have write your shopping cart has been clear this message will be display on web page after clear all item from shopping cart now friends first we have add some item into shopping cart now we want to remove all item from shopping cart so we have click on this clear all link after click on this link we have removed success message like your shopping cart has been clear message on web page so this way we can remove all item from shopping cart in single click so in this video tutorial we have seen how can we add item into shopping cart and then after display that shopping cart details on web page in table format with complete calculation after this we have seen if we have add same item into cart then at that time we have changed particular item quantity into cart instead of whole item details add into cart then after we have seen how can we remove single item from shopping cart and lastly we have seen how to remove or how to clear shopping cart in single click this all feature we have done by using PHP cookie variable instead of session variable so if you want to learn how to make PHP shopping cart by using cookie then this tutorial will helpful if you have any query regarding this video tutorial part please comment your query in comment box and if you like this video tutorial please share with your friends or even you can also share on social media also if you want to get more update regarding our video tutorial please subscribe our YouTube channel forget more update regarding release of future video lastly keep watching our YouTube channel thanks for watching this video tutorial
Info
Channel: Webslesson
Views: 31,898
Rating: undefined out of 5
Keywords: add to cart using cookies php, shopping cart cookie, how to make shopping cart in php cookies, php shopping cart tutorial using cookies, php shopping cart using cookies, php shopping cart tutorial step by step, shopping cart using cookies, how to make shopping cart in php, how to make shopping cart in php mysql, simple shopping cart in php, php shopping cart, make shopping cart, cart in php, shopping cart in php, php shopping cart source code
Id: -zE_rQZH8Wk
Channel Id: undefined
Length: 27min 28sec (1648 seconds)
Published: Tue May 08 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.