php SESSION application example (shopping cart)

SESSION Application Example

Login example: (Please note that you must type it yourself, no CV method)
First, look at the results chart to stimulate students' desire to write. The login page is as follows:

document_2015-09-04_55e9473ab4ec7.png

Explain what the problem is. Next, implement it yourself.
First database information:
Create a new database named login, and then create a user table. The structure of the table is as follows:

document_2015-09-04_55e9487784049.png

##login.php

     
用户名: 密 码:

welcome.php Here we use the information in the session, instead of bringing parameters in the URL like cookies.

   
welcome,

Example of shopping cart: (Please note that you must type it yourself, do not CV Dafa)

Database information: Create a database named test. There is a shop table in the library. The table structure is as follows:

document_2015-09-04_55e9496188de0.png

Start Let’s code!goodsList.php This is the product display page. The rendering is as follows:
Explain that if it is the first time to purchase an item, add the product information to the shopping cart and calculate the total price. If it is purchased again, Click to purchase, the quantity of purchased items will be increased by 1, and the total price will be recalculated. View the shopping cart link to go to the shopping cart page.

     
购买"; echo '
'; } ?> 查看购物车

buy.php This page completes the purchase function and then jumps to the product list again. The main purpose is to process the purchase of goods in the session.

   

shoppingCart.php This page displays the products, prices, total price and other information in the shopping cart.

The renderings are as follows:

document_2015-09-04_55e945fc2e667.png

   
'; foreach ($goods as $value) { echo $value['name'] . ' 价格 ' . $value['price'] . ' 数量 ' . $value['number'] . '
'; } echo '总价:' . $_SESSION['totalPrice'] . '
'; ?> 返回商品列表

The shopping cart example is completed. Do you feel a sense of accomplishment after completing it yourself? ! you're good! !

Continuing Learning
||
'; foreach ($goods as $value) { echo $value['name'] . ' 价格 ' . $value['price'] . ' 数量 ' . $value['number'] . '
'; } echo '总价:' . $_SESSION['totalPrice'] . '
'; ?> 返回商品列表
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!