1、登錄頁面php
封裝BDDAcss
1
|
<?php
class
DBDA{
public
$host
=
"localhost"
;
//服務器地址
public
$uid
=
"root"
;
//用戶名
public
$pwd
=
"123"
;
//密碼
public
$dbname
=
"shopping"
;
//數據庫名稱
/*
執行一條SQL語句的方法
@param sql 要執行的SQL語句
@param type SQL語句的類型,0表明查詢 1表明增刪改
@return 若是是查詢語句返回二維數組,若是是增刪改返回true或false
*/
public
function
query(
$sql
,
$type
=0){
$db
=
new
MySQLi(
$this
->host,
$this
->uid,
$this
->pwd,
$this
->dbname);
$result
=
$db
->query(
$sql
);
if
(
$type
){
return
$result
;
}
else
{
return
$result
->fetch_all();
}
}
public
function
strquery(
$sql
,
$type
=0){
$db
=
new
MySQLi(
$this
->host,
$this
->uid,
$this
->pwd,
$this
->dbname);
$result
=
$db
->query(
$sql
);
if
(
$type
){
return
$result
;
}
else
{
$arr
=
$result
->fetch_all();
$str
=
""
;
foreach
(
$arr
as
$v
){
$str
.= implode(
"^"
,
$v
).
"|"
;
}
$str
=
substr
(
$str
,0,
strlen
(
$str
)-1);
return
$str
;
}
}
//返回json數據的方法
public
function
jsonquery(
$sql
,
$type
=0){
$db
=
new
MySQLi(
$this
->host,
$this
->uid,
$this
->pwd,
$this
->dbname);
$result
=
$db
->query(
$sql
);
if
(
$type
){
return
$result
;
}
else
{
$arr
=
$result
->fetch_all(MYSQLI_ASSOC);
//關聯數組
return
json_encode(
$arr
);
//轉換json
//json_decode()分解json
}
}
}
|
css樣式表html
*{
margin
:
0px
auto
;
padding
:
0px
;
}
#login{
background-color
:
#0CF
;
width
:
30%
;
height
:
200px
;
margin-top
:
50px
;
border
:
5px
double
#060
;
}
#dl{
color
:
#00F
;
background-color
:
#9FF
;
height
:
30px
;
text-indent
:
10px
;
vertical-align
:
bottom
;
line-height
:
30px
;
}
#pwd{
background-color
:
#CFF
;
height
:
70px
;
}
#sb{
background-color
:
#060
;
width
:
200px
;
height
:
30px
;
color
:
#CFF
;
margin-top
:
3px
;
}
|
佈局樣式sql
|
<body>
<form method=
"post"
action=
"dengluchuli.php"
>
<div id=
"login"
>
<div align=
"left"
id=
"dl"
>登陸頁面</div><br />
<div id=
"yhm"
align=
"center"
>用戶名:
<input type=
"text"
placeholder=
"請輸入用戶名"
name=
"uid"
/></div><br />
<div id=
"pwd"
align=
"center"
>密 碼:
<input type=
"password"
placeholder=
"請輸入密碼"
name=
"pwd"
/></div>
<div align=
"center"
><input type=
"submit"
value=
"登陸"
id=
"sb"
/></div>
</div>
</form>
</body>
|
2、登陸處理數據庫
<?php
session_start();
require_once
"../DBDA.class.php"
;
$db
=
new
DBDA();
$uid
=
$_POST
[
"uid"
];
$pwd
=
$_POST
[
"pwd"
];
$sql
=
"select password from login where username='{$uid}'"
;
$mm
=
$db
->strquery(
$sql
);
if
(!
empty
(
$pwd
) &&
$pwd
==
$mm
){
$_SESSION
[
"uid"
] =
$uid
;
header(
"location:main.php"
);
}
else
{
header(
"location:denglu.php"
);
}
|
3、主頁的css樣式json
|
*{
margin
:
0px
auto
;
padding
:
0px
;
}
#shang{
background-color
:
#0FF
;
width
:
100%
;
height
:
60px
;
vertical-align
:
bottom
;
line-height
:
90px
;
text-indent
:
10px
;
}
#zuo{
float
:
left
;
background-color
:
#999
;
width
:
30%
;
height
:
500px
;
}
.caidan{
width
:
80%
;
height
:
60px
;
background-color
:
#69F
;
margin-top
:
20px
;
text-align
:
center
;
vertical-align
:
middle
;
line-height
:
60px
;
font-weight
:
bold
;
}
#yous{
height
:
40px
;
width
:
100%
;
background-color
:
#FFF
;
vertical-align
:
bottom
;
line-height
:
60px
;
text-indent
:
10px
;
color
:
#009
;
font-size
:
20px
;
}
#you{
background-color
:
#06F
;
width
:
70%
;
height
:
500px
;
float
:
left
;
}
table{
color
:
#CF0
;
text-align
:
center
;
}
|
主頁佈局數組
|
<
body
>
<?
php
session_start();
if(empty($_SESSION["uid"])){
header("location:denglu.php");
exit;
}
?>
<
div
>
<
div
id="shang"><
h2
>超牛逼購物網</
h2
></
div
>
<
div
>
<
div
id="zuo">
<
div
class="caidan"><
a
href="main.php">瀏覽商品</
a
></
div
>
<
div
class="caidan"><
a
href="zhanghu.php">查看帳戶</
a
></
div
>
<
div
class="caidan"><
a
href="gouwuche.php">查看購物車</
a
></
div
>
</
div
>
<
div
id="you">
<
div
id="yous">
<?
php
require_once"../DBDA.class.php";
$db = new DBDA();
if(!empty($_SESSION["gwc"])){
$arr = $_SESSION["gwc"];
$count = count($arr);//購物車中商品的數量
$sum = 0;//商品總價
foreach($arr as $v){
$sql = "select price from fruit where ids ='{$v[0]}'";
$danjia = $db->strquery($sql);
$sum +=$danjia*$v[1];
}
echo "購物車中總共有{$count}種商品,總價爲:{$sum}元";
}
?>
</
div
>
<
table
border="1" bordercolor="#CCFFFF" width="100%">
<
tr
bgcolor="#339999" height="40">
<
td
>水果代號</
td
>
<
td
>水果名稱</
td
>
<
td
>水果價格</
td
>
<
td
>水果產地</
td
>
<
td
>貨架</
td
>
<
td
>庫存量</
td
>
<
td
>操做</
td
>
</
tr
>
<?
php
$sql = "select * from fruit";
$arr = $db->query($sql);
foreach($arr as $v){
echo"<
tr
>
<
td
>{$v[0]}</
td
>
<
td
>{$v[1]}</
td
>
<
td
>{$v[2]}</
td
>
<
td
>{$v[3]}</
td
>
<
td
>{$v[4]}</
td
>
<
td
>{$v[5]}</
td
>
<
td
><
a
href='tianjia.php?code={$v[0]}'><
input
type='button' value='添加購物車'/></
a
></
td
>
</
tr
>";
}
?>
</
table
>
</
div
>
</
div
>
</
div
>
</
body
>
|
帳號信息服務器
|
<?
php
session_start();
if(empty($_SESSION["uid"])){
header("location:login.php");
exit;
}
?>
<
body
>
<
div
>
<
div
id="shang"><
h2
>帳戶信息</
h2
></
div
>
<
div
>
<
div
id="zuo">
<
div
class="caidan"><
a
href="main.php">瀏覽商品</
a
></
div
>
<
div
class="caidan"><
a
href="zhanghu.php">查看帳戶</
a
></
div
>
<
div
class="caidan"><
a
href="gouwuche.php">查看購物車</
a
></
div
>
</
div
>
<
div
id="you">
<
div
id="yous">帳戶基本信息</
div
>
<
table
border="1" bordercolor="#CCFFFF" width="100%">
<
tr
bgcolor="#339999" height="40">
<
td
>用戶名</
td
>
<
td
>姓名</
td
>
<
td
>帳戶餘額</
td
>
</
tr
>
<?
php
$uid = $_SESSION["uid"];
require_once "../DBDA.class.php";
$db = new DBDA();
$sql = "select * from login where username = '{$uid}'";
$arr = $db->query($sql);
foreach($arr as $v){
echo"<
tr
>
<
td
>{$v[0]}</
td
>
<
td
>{$v[1]}</
td
>
<
td
>{$v[3]}</
td
>
</
tr
>";
}
?>
</
table
>
</
div
>
</
div
>
</
div
>
</
body
>
|
購物車session
|
<!
doctype
html>
<
html
>
<
head
>
<
meta
charset="utf-8">
<
title
>無標題文檔</
title
>
<
link
href="main.css" rel="stylesheet" type="text/css">
<
style
type="text/css">
#st{
color:#FFF;
font-size:20px;
margin-left:450px;
}
</
style
>
</
head
>
<?
php
session_start();
if(empty($_SESSION["uid"])){
header("location:login.php");
exit;
}
?>
<
body
>
<
form
action="dingdantijiao.php" method="post">
<
div
>
<
div
id="shang"><
h2
>購物車</
h2
></
div
>
<
div
>
<
div
id="zuo">
<
div
class="caidan"><
a
href="main.php">瀏覽商品</
a
></
div
>
<
div
class="caidan"><
a
href="zhanghu.php">查看帳戶</
a
></
div
>
<
div
class="caidan"><
a
href="gouwuche.php">查看購物車</
a
></
div
>
</
div
>
<
div
id="you">
<
div
id="yous">購物車有如下商品</
div
>
<
table
border="1" bordercolor="#CCFFFF" width="100%">
<
tr
bgcolor="#339999" height="40">
<
td
>商品名稱</
td
>
<
td
>商品單價</
td
>
<
td
>購買數量</
td
>
<
td
>操做</
td
>
</
tr
>
<?
php
require_once "../DBDA.class.php";
$db = new DBDA();
$sql = "select * from ordertails";
$arr = $_SESSION["gwc"];
foreach($arr as $v){
$sql = "select name,price from fruit where ids='{$v[0]}'";
$name = $db->query($sql);
echo "<
tr
>
<
td
>{$name[0][0]}</
td
>
<
td
>{$name[0][1]}</
td
>
<
td
>{$v[1]}</
td
>
<
td
><
a
href='del.php?code={$v[0]}'><
input
type='button' value='刪除' /></
a
></
td
>
</
tr
>";
}
?>
</
table
>
<
br
>
<
a
href="dingdantijiao.php?ids={$v[0]}" rel="external nofollow" id="st">提交訂單</
a
>
</
div
>
</
div
>
</
div
>
</
form
>
</
body
>
</
html
>
|
刪除商品處理佈局
|
<?php
session_start();
$code
=
$_GET
[
"code"
];
$arr
=
$_SESSION
[
"gwc"
];
//var_dump($arr);
//取索引2(數量)
foreach
(
$arr
as
$k
=>
$v
)
{
if
(
$v
[0]==
$code
)
{
if
(
$v
[1]>1){
//要刪除的數據
$arr
[
$k
][1]-=1;
}
else
{
//數量爲1的狀況下,移除該數組
unset(
$arr
[
$k
]);
}
}
}
$_SESSION
[
"gwc"
] =
$arr
;
//記得扔到session裏面
header(
"location:gouwuche.php"
);
//刪除完跳轉回去
|
提交訂單
|
<?php
session_start();
$ids
=
$_GET
[
"ids"
];
//查看餘額
$uid
=
$_SESSION
[
"uid"
];
require_once
"../DBDA.class.php"
;
$db
=
new
DBDA();
$sql
=
"select account from login where username='{$uid}'"
;
$arr
=
$db
->query(
$sql
,0);
$aye
=
$arr
[0][0];
//餘額
//var_dump($aye);
if
(!
empty
(
$_SESSION
[
"gwc"
])){
$arr
=
$_SESSION
[
"gwc"
];
$sum
= 0;
//$numbers = count($arr);
foreach
(
$arr
as
$v
){
$sql
=
"select * from fruit where ids='{$v[0]}'"
;
$price
=
$db
->query(
$sql
,0);
$dj
=
$price
[0][2];
$sum
=
$sum
+
$dj
*
$v
[1];
}
}
else
{
echo
"您還未購買商品!"
;
//header("shopping_list.php");
exit
;
}
//判斷餘額是否知足購買
if
(
$aye
>=
$sum
){
//判斷庫存
foreach
(
$arr
as
$v
){
$skc
=
"select name,numbers from fruit where ids='{$v[0]}'"
;
$akc
=
$db
->query(
$sql
,0);
//var_dump($akc);
$kc
=
$akc
[0][4];
//庫存
//var_dump($kc);
if
(
$kc
<
$v
[1]){
echo
"庫存不足!"
;
exit
;
}
}
//提交訂單
//帳戶扣除餘額
$skye
=
"update login set account=account-'{$sum}' where username='{$uid}'"
;
$zhye
=
$db
->query(
$skye
,1);
//扣除庫存
foreach
(
$arr
as
$v
){
$skckc
=
"update fruit set numbers=numbers-'{$v[1]}' where ids='{$v[0]}'"
;
$sykc
=
$db
->query(
$skckc
,1);
}
//添加訂單
$ddh
=
date
(
"Y-m-d H:i:s"
);
$time
= time();
$stjd
=
"insert into orders values('{$time}','{$uid}','{$ddh}')"
;
$wcdh
=
$db
->query(
$stjd
,1);
//添加訂單詳情
foreach
(
$arr
as
$v
){
$ddxq
=
"insert into orderdetails values('','{$ddh}','{$v[0]}','{$v[1]}')"
;
$axq
=
$db
->query(
$ddxq
,1);
}
}
else
{
echo
"餘額不足,請充值!"
;
exit
;
}
unset(
$_SESSION
[
"gwc"
]);
header(
"location:main.php"
);
|