亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

<?php
session_start();
if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) {
   $userName = $_POST['username'];
   $password = $_POST['password'];

   //從db獲取用戶信息   數(shù)據(jù)庫信息改成自己的
   $conn = mysqli_connect('host','username','password','login');
   $res = mysqli_query($conn,"select * from user where `username` =  '$username' ");
   $row = mysqli_fetch_assoc($res);
   if ($row['password'] == $password) {
       //密碼驗證通過,設(shè)置session,把用戶名和密碼保存在服務(wù)端
       $_SESSION['username'] = $username;
       $_SESSION['password'] = $password;

       //最后跳轉(zhuǎn)到登錄后的歡迎頁面 //注意:這里我們沒有像cookie一樣帶參數(shù)過去
       header('Location: welcome.php');
   }
}

?>
<html>
<head>
<!-- 這里指明頁面編碼 -->
<meta charset="utf-8">
</head>
<body>
   <form action="" method="POST">
       <div>
           用戶名:<input type="text" name="username" />
           密  碼:<input type="text" name="password" />
           <input type="submit" value="登錄">        
       </div>
   </form>
</body>
</html>

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

<?php
session_start();
$username = $_SESSION['username'];
?>
<html>
<head>

</head>
<body>
   welcome,<?php echo $username;?>
</body>
</html>

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.

<?php
   $goods = array();
   //從數(shù)據(jù)庫獲取商品信息存入$goods二維數(shù)組
   $i = 0;
   //這里請換上自己的數(shù)據(jù)庫相關(guān)信息
   $conn = mysqli_connect('host','username','password','test');
   $res = mysqli_query($conn,'select * from shop');
   //這里把商品信息放到$goods二維數(shù)組,每一維存的是單個
   //商品的信息,比如商品名、價格。
   while ($row = mysqli_fetch_assoc($res)) {
       $goods[$i]['id'] = $row['id'];
       $goods[$i]['name'] = $row['name'];
       $goods[$i]['price'] = $row['price'];
       $i++ ;
   }

?>
<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
   <?php
   //取出商品信息顯示在頁面上,并添加購買功能
       foreach ($goods as $value) {
           echo ' 商品名 ' . $value['name'] . ' 價格 ' . $value['price'];
           echo "<a href=buy.php?name=" . $value['name'] . '&price=' . $value['price'] .">購買</a>";
           echo '<br />';
       }

   ?>
   <a href="shoppingCart.php">查看購物車</a>
</body>
</html>

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.

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<?php
   //開啟session
   session_start();

   //獲取傳過來的商品名和價格
   $name = $_GET['name'];
   $price = $_GET['price'];

   //把session中的商品信息和傳過來的(剛買的)商品信息對比
   $goods = $_SESSION['goods'];
   if ($name == $goods[$name]['name']) {
       //買過的話,則總價格增加,相應(yīng)商品數(shù)量增加
       $_SESSION['totalPrice'] += $price;
       $goods[$name]['number'] += 1;
   } else {
       //第一次買的話,將相應(yīng)的商品信息添加到session中
       $goods[$name]['name'] = $name;
       $goods[$name]['price'] = $price;
       $goods[$name]['number'] += 1;
       $_SESSION['totalPrice'] += $price;
   }

   $_SESSION['goods'] = $goods;
   //購買處理完畢后跳轉(zhuǎn)到商品列表
   header('location: goodsList.php');
?>
</body>
</html>

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

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<?php
session_start();
//將session中的商品信息(即購物車中的商品)和總價顯示到頁面
$goods = $_SESSION['goods'];
echo '您買了:<br />';
foreach ($goods as $value) {
   echo $value['name'] . ' 價格 ' . $value['price'] . ' 數(shù)量 ' . $value['number'] . '<br />';
}
echo '總價:' . $_SESSION['totalPrice'] . '<br />';

?>
<a href="goodsList.php">返回商品列表</a>
</body>
</html>

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

Continuing Learning
||
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <?php session_start(); //將session中的商品信息(即購物車中的商品)和總價顯示到頁面 $goods = $_SESSION['goods']; echo '您買了:<br />'; foreach ($goods as $value) { echo $value['name'] . ' 價格 ' . $value['price'] . ' 數(shù)量 ' . $value['number'] . '<br />'; } echo '總價:' . $_SESSION['totalPrice'] . '<br />'; ?> <a href="goodsList.php">返回商品列表</a> </body> </html>
submitReset Code