PHP สร้างระบบตะกร้า Shopping Cart ด้วย Session และ Array สำหรับตัวอย่าง Script นี้เป็นการสร้างระบบตะกร้าสินค้า หรือ Shopping Cart แบบง่าย ๆ โดยหลักการก็คือดึงข้อมูลจาก MySQL มาแสดง และให้ผู้ใช้ได้ทำการเลือกสินค้า ลงในตะกร้า โดยใช้ระบบจัดเก็บแบบ PHP Array ผ่าน Session โดยลูกค้าสามารถทำการเลือกสินค้าอื่น ๆ ได้ตลอดเวลาที่อยู่ในหน้าจอบนเว็บ โดยในตัวอย่างเมื่อลูกค้าได้เลือกสินค้าซ้ำกับสินค้าที่ได้เลือกแล้วก็จะมี การบวกหนึ่งให้อัตโนมัติ และสามารถลบรายการที่ได้เลือกไว้แล้ว รวมทั้งหน้าจอสำหรับ Checkout การกรอก ข้อมูลชื่อและที่อยู่ในการจัดส่งสินค้า และสุดท้ายหลังจากทำการ Checkout แล้วก็จะแสดงข้อมูลที่ได้ถูกกจัดเก็บลงในฐานข้อมูล MySQL พร้อมกับหมายเลข OrderID เพื่ออ้างอิง
Screenshot
คุณสมบัติและความสามารถของตัวอย่างนี้
- แสดงรายละเอียดสินค้าจากฐานข้อมูล MySQL
- จัดเก็บข้อมูลการสั่งซื้อในรูปแบบ PHP และ Session
- หน้าจอสำหรับ Checkout
- หน้าจอแสดงรายละเอียดการสั่งซื้อ
ตารางประกอบด้วย 3 ตารางคือ
- product
- orders
- orders_detail
- orders
- orders_detail
โครงสร้างตาราง product เก็บรายละเอียดสินค้า
โครงสร้างตาราง orders เก็บข้อมูลลูกค้าที่สั่งซื้อ และหมายเลข OrderID
โครงสร้างตาราง orders_detail เก็บรายละเอียดของแต่ล่ะ OrderID
ตารางของ MySQL
--
-- Table structure for table `orders`
--
CREATE TABLE `orders` (
`OrderID` int(5) unsigned zerofill NOT NULL auto_increment,
`OrderDate` datetime NOT NULL,
`Name` varchar(100) NOT NULL,
`Address` varchar(500) NOT NULL,
`Tel` varchar(100) NOT NULL,
`Email` varchar(100) NOT NULL,
PRIMARY KEY (`OrderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `orders`
--
INSERT INTO `orders` VALUES (00001, '2012-03-15 09:59:13', 'Weerachai Nukitram', '1234 Lapharo Bangkok Thailand', '0819876107', 'is_php@hotmail.com');
INSERT INTO `orders` VALUES (00002, '2012-03-15 10:15:03', 'Weerachai Nukitram', '1234 Latpharo Bangkok Thailand', '0819876107', 'is_php@hotmail.com');
-- --------------------------------------------------------
--
-- Table structure for table `orders_detail`
--
CREATE TABLE `orders_detail` (
`DetailID` int(5) NOT NULL auto_increment,
`OrderID` int(5) unsigned zerofill NOT NULL,
`ProductID` int(4) NOT NULL,
`Qty` int(3) NOT NULL,
PRIMARY KEY (`DetailID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `orders_detail`
--
INSERT INTO `orders_detail` VALUES (1, 00001, 4, 1);
INSERT INTO `orders_detail` VALUES (2, 00002, 3, 3);
INSERT INTO `orders_detail` VALUES (3, 00002, 1, 1);
INSERT INTO `orders_detail` VALUES (4, 00002, 4, 1);
-- --------------------------------------------------------
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`ProductID` int(4) NOT NULL auto_increment,
`ProductName` varchar(100) NOT NULL,
`Price` double NOT NULL,
`Picture` varchar(100) NOT NULL,
PRIMARY KEY (`ProductID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `product`
--
INSERT INTO `product` VALUES (1, 'Product 1', 100, '1.gif');
INSERT INTO `product` VALUES (2, 'Product 2', 200, '2.gif');
INSERT INTO `product` VALUES (3, 'Product 3', 300, '3.gif');
INSERT INTO `product` VALUES (4, 'Product 4', 400, '4.gif');
-- Table structure for table `orders`
--
CREATE TABLE `orders` (
`OrderID` int(5) unsigned zerofill NOT NULL auto_increment,
`OrderDate` datetime NOT NULL,
`Name` varchar(100) NOT NULL,
`Address` varchar(500) NOT NULL,
`Tel` varchar(100) NOT NULL,
`Email` varchar(100) NOT NULL,
PRIMARY KEY (`OrderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `orders`
--
INSERT INTO `orders` VALUES (00001, '2012-03-15 09:59:13', 'Weerachai Nukitram', '1234 Lapharo Bangkok Thailand', '0819876107', 'is_php@hotmail.com');
INSERT INTO `orders` VALUES (00002, '2012-03-15 10:15:03', 'Weerachai Nukitram', '1234 Latpharo Bangkok Thailand', '0819876107', 'is_php@hotmail.com');
-- --------------------------------------------------------
--
-- Table structure for table `orders_detail`
--
CREATE TABLE `orders_detail` (
`DetailID` int(5) NOT NULL auto_increment,
`OrderID` int(5) unsigned zerofill NOT NULL,
`ProductID` int(4) NOT NULL,
`Qty` int(3) NOT NULL,
PRIMARY KEY (`DetailID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `orders_detail`
--
INSERT INTO `orders_detail` VALUES (1, 00001, 4, 1);
INSERT INTO `orders_detail` VALUES (2, 00002, 3, 3);
INSERT INTO `orders_detail` VALUES (3, 00002, 1, 1);
INSERT INTO `orders_detail` VALUES (4, 00002, 4, 1);
-- --------------------------------------------------------
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`ProductID` int(4) NOT NULL auto_increment,
`ProductName` varchar(100) NOT NULL,
`Price` double NOT NULL,
`Picture` varchar(100) NOT NULL,
PRIMARY KEY (`ProductID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `product`
--
INSERT INTO `product` VALUES (1, 'Product 1', 100, '1.gif');
INSERT INTO `product` VALUES (2, 'Product 2', 200, '2.gif');
INSERT INTO `product` VALUES (3, 'Product 3', 300, '3.gif');
INSERT INTO `product` VALUES (4, 'Product 4', 400, '4.gif');
นำ Query ไปรันหรือสร้าง Database ตามโครงสร้าง
Code ทั้งหมด
product.php
01.
<?
02.
//session_start();
03.
//session_destroy();
04.
?><html>
05.
<head>
06.
<title>ThaiCreate.Com</title>
07.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
08.
</head>
09.
<?
10.
mysql_connect(
"localhost"
,
"root"
,
"root"
);
11.
mysql_select_db(
"mydatabase"
);
12.
$strSQL
=
"SELECT * FROM product"
;
13.
$objQuery
= mysql_query(
$strSQL
)
or
die
(mysql_error());
14.
?>
15.
<table width=
"327"
border=
"1"
>
16.
<tr>
17.
<td width=
"101"
>Picture</td>
18.
<td width=
"101"
>ProductID</td>
19.
<td width=
"82"
>ProductName</td>
20.
<td width=
"79"
>Price</td>
21.
<td width=
"37"
>Cart</td>
22.
</tr>
23.
<?
24.
while
(
$objResult
= mysql_fetch_array(
$objQuery
))
25.
{
26.
?>
27.
<tr>
28.
<td><img src=
"img/<?=$objResult["
Picture
"];?>"
></td>
29.
<td><?=
$objResult
[
"ProductID"
];?></td>
30.
<td><?=
$objResult
[
"ProductName"
];?></td>
31.
<td><?=
$objResult
[
"Price"
];?></td>
32.
<td><a href=
"order.php?ProductID=<?=$objResult["
ProductID
"];?>"
>Order</a></td>
33.
</tr>
34.
<?
35.
}
36.
?>
37.
</table>
38.
<br><br><a href=
"show.php"
>View Cart</a> | <a href=
"clear.php"
>Clear Cart</a>
39.
<?
40.
mysql_close();
41.
?>
42.
</body>
43.
</html>
order.php
01.
<?
02.
ob_start();
03.
session_start();
04.
05.
if
(!isset(
$_SESSION
[
"intLine"
]))
06.
{
07.
08.
$_SESSION
[
"intLine"
] = 0;
09.
$_SESSION
[
"strProductID"
][0] =
$_GET
[
"ProductID"
];
10.
$_SESSION
[
"strQty"
][0] = 1;
11.
12.
header(
"location:show.php"
);
13.
}
14.
else
15.
{
16.
17.
$key
=
array_search
(
$_GET
[
"ProductID"
],
$_SESSION
[
"strProductID"
]);
18.
if
((string)
$key
!=
""
)
19.
{
20.
$_SESSION
[
"strQty"
][
$key
] =
$_SESSION
[
"strQty"
][
$key
] + 1;
21.
}
22.
else
23.
{
24.
25.
$_SESSION
[
"intLine"
] =
$_SESSION
[
"intLine"
] + 1;
26.
$intNewLine
=
$_SESSION
[
"intLine"
];
27.
$_SESSION
[
"strProductID"
][
$intNewLine
] =
$_GET
[
"ProductID"
];
28.
$_SESSION
[
"strQty"
][
$intNewLine
] = 1;
29.
}
30.
31.
header(
"location:show.php"
);
32.
33.
}
34.
?>
show.php
01.
<?
02.
session_start();
03.
?>
04.
<html>
05.
<head>
06.
<title>ThaiCreate.Com</title>
07.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
08.
</head>
09.
<?
10.
mysql_connect(
"localhost"
,
"root"
,
"root"
);
11.
mysql_select_db(
"mydatabase"
);
12.
?>
13.
<table width=
"400"
border=
"1"
>
14.
<tr>
15.
<td width=
"101"
>ProductID</td>
16.
<td width=
"82"
>ProductName</td>
17.
<td width=
"82"
>Price</td>
18.
<td width=
"79"
>Qty</td>
19.
<td width=
"79"
>Total</td>
20.
<td width=
"10"
>Del</td>
21.
</tr>
22.
<?
23.
$Total
= 0;
24.
$SumTotal
= 0;
25.
26.
for
(
$i
=0;
$i
<=(int)
$_SESSION
[
"intLine"
];
$i
++)
27.
{
28.
if
(
$_SESSION
[
"strProductID"
][
$i
] !=
""
)
29.
{
30.
$strSQL
=
"SELECT * FROM product WHERE ProductID = '"
.
$_SESSION
[
"strProductID"
][
$i
].
"' "
;
31.
$objQuery
= mysql_query(
$strSQL
)
or
die
(mysql_error());
32.
$objResult
= mysql_fetch_array(
$objQuery
);
33.
$Total
=
$_SESSION
[
"strQty"
][
$i
] *
$objResult
[
"Price"
];
34.
$SumTotal
=
$SumTotal
+
$Total
;
35.
?>
36.
<tr>
37.
<td><?=
$_SESSION
[
"strProductID"
][
$i
];?></td>
38.
<td><?=
$objResult
[
"ProductName"
];?></td>
39.
<td><?=
$objResult
[
"Price"
];?></td>
40.
<td><?=
$_SESSION
[
"strQty"
][
$i
];?></td>
41.
<td><?=number_format(
$Total
,2);?></td>
42.
<td><a href=
"delete.php?Line=<?=$i;?>"
>x</a></td>
43.
</tr>
44.
<?
45.
}
46.
}
47.
?>
48.
</table>
49.
Sum Total <?=number_format(
$SumTotal
,2);?>
50.
<br><br><a href=
"product.php"
>Go to Product</a>
51.
<?
52.
if
(
$SumTotal
> 0)
53.
{
54.
?>
55.
| <a href=
"checkout.php"
>CheckOut</a>
56.
<?
57.
}
58.
?>
59.
<?
60.
mysql_close();
61.
?>
62.
</body>
63.
</html>
delete.php
01.
<?
02.
ob_start();
03.
session_start();
04.
05.
$Line
=
$_GET
[
"Line"
];
06.
$_SESSION
[
"strProductID"
][
$Line
] =
""
;
07.
$_SESSION
[
"strQty"
][
$Line
] =
""
;
08.
09.
header(
"location:show.php"
);
10.
?>
checkout.php
01.
<?
02.
session_start();
03.
?>
04.
<html>
05.
<head>
06.
<title>ThaiCreate.Com</title>
07.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
08.
</head>
09.
<?
10.
mysql_connect(
"localhost"
,
"root"
,
"root"
);
11.
mysql_select_db(
"mydatabase"
);
12.
?>
13.
<table width=
"400"
border=
"1"
>
14.
<tr>
15.
<td width=
"101"
>ProductID</td>
16.
<td width=
"82"
>ProductName</td>
17.
<td width=
"82"
>Price</td>
18.
<td width=
"79"
>Qty</td>
19.
<td width=
"79"
>Total</td>
20.
</tr>
21.
<?
22.
$Total
= 0;
23.
$SumTotal
= 0;
24.
25.
for
(
$i
=0;
$i
<=(int)
$_SESSION
[
"intLine"
];
$i
++)
26.
{
27.
if
(
$_SESSION
[
"strProductID"
][
$i
] !=
""
)
28.
{
29.
$strSQL
=
"SELECT * FROM product WHERE ProductID = '"
.
$_SESSION
[
"strProductID"
][
$i
].
"' "
;
30.
$objQuery
= mysql_query(
$strSQL
)
or
die
(mysql_error());
31.
$objResult
= mysql_fetch_array(
$objQuery
);
32.
$Total
=
$_SESSION
[
"strQty"
][
$i
] *
$objResult
[
"Price"
];
33.
$SumTotal
=
$SumTotal
+
$Total
;
34.
?>
35.
<tr>
36.
<td><?=
$_SESSION
[
"strProductID"
][
$i
];?></td>
37.
<td><?=
$objResult
[
"ProductName"
];?></td>
38.
<td><?=
$objResult
[
"Price"
];?></td>
39.
<td><?=
$_SESSION
[
"strQty"
][
$i
];?></td>
40.
<td><?=number_format(
$Total
,2);?></td>
41.
</tr>
42.
<?
43.
}
44.
}
45.
?>
46.
</table>
47.
Sum Total <?=number_format(
$SumTotal
,2);?>
48.
<br><br>
49.
<form name=
"form1"
method=
"post"
action=
"save_checkout.php"
>
50.
<table width=
"304"
border=
"1"
>
51.
<tr>
52.
<td width=
"71"
>Name</td>
53.
<td width=
"217"
><input type=
"text"
name=
"txtName"
></td>
54.
</tr>
55.
<tr>
56.
<td>Address</td>
57.
<td><textarea name=
"txtAddress"
></textarea></td>
58.
</tr>
59.
<tr>
60.
<td>Tel</td>
61.
<td><input type=
"text"
name=
"txtTel"
></td>
62.
</tr>
63.
<tr>
64.
<td>Email</td>
65.
<td><input type=
"text"
name=
"txtEmail"
></td>
66.
</tr>
67.
</table>
68.
<input type=
"submit"
name=
"Submit"
value=
"Submit"
>
69.
</form>
70.
<?
71.
mysql_close();
72.
?>
73.
</body>
74.
</html>
save_checkout.php
01.
<?
02.
session_start();
03.
04.
mysql_connect(
"localhost"
,
"root"
,
"root"
);
05.
mysql_select_db(
"mydatabase"
);
06.
07.
$Total
= 0;
08.
$SumTotal
= 0;
09.
10.
$strSQL
= "
11.
INSERT INTO orders (OrderDate,Name,Address,Tel,Email)
12.
VALUES
13.
(
'".date("Y-m-d H:i:s")."'
,
'".$_POST["txtName"]."'
,
'".$_POST["txtAddress"]."'
,
'".$_POST["txtTel"]."'
,
'".$_POST["txtEmail"]."'
)
14.
";
15.
mysql_query(
$strSQL
)
or
die
(mysql_error());
16.
17.
$strOrderID
= mysql_insert_id();
18.
19.
for
(
$i
=0;
$i
<=(int)
$_SESSION
[
"intLine"
];
$i
++)
20.
{
21.
if
(
$_SESSION
[
"strProductID"
][
$i
] !=
""
)
22.
{
23.
$strSQL
= "
24.
INSERT INTO orders_detail (OrderID,ProductID,Qty)
25.
VALUES
26.
(
'".$strOrderID."'
,
'".$_SESSION["strProductID"][$i]."'
,
'".$_SESSION["strQty"][$i]."'
)
27.
";
28.
mysql_query(
$strSQL
)
or
die
(mysql_error());
29.
}
30.
}
31.
32.
mysql_close();
33.
34.
session_destroy();
35.
36.
header(
"location:finish_order.php?OrderID="
.
$strOrderID
);
37.
?>
finish_order.php
01.
<html>
02.
<head>
03.
<title>ThaiCreate.Com</title>
04.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
05.
</head>
06.
<body>
07.
Finish Your Order. <br><br>
08.
09.
<a href=
"view_order.php?OrderID=<?=$_GET["
OrderID
"];?>"
>View Order</a>
10.
11.
</body>
12.
</html>
view_order.php
01.
<html>
02.
<head>
03.
<title>ThaiCreate.Com</title>
04.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
05.
</head>
06.
<?
07.
mysql_connect(
"localhost"
,
"root"
,
"root"
);
08.
mysql_select_db(
"mydatabase"
);
09.
10.
$strSQL
=
"SELECT * FROM orders WHERE OrderID = '"
.
$_GET
[
"OrderID"
].
"' "
;
11.
$objQuery
= mysql_query(
$strSQL
)
or
die
(mysql_error());
12.
$objResult
= mysql_fetch_array(
$objQuery
);
13.
?>
14.
15.
<table width=
"304"
border=
"1"
>
16.
<tr>
17.
<td width=
"71"
>OrderID</td>
18.
<td width=
"217"
>
19.
<?=
$objResult
[
"OrderID"
];?></td>
20.
</tr>
21.
<tr>
22.
<td width=
"71"
>Name</td>
23.
<td width=
"217"
>
24.
<?=
$objResult
[
"Name"
];?></td>
25.
</tr>
26.
<tr>
27.
<td>Address</td>
28.
<td><?=
$objResult
[
"Address"
];?></td>
29.
</tr>
30.
<tr>
31.
<td>Tel</td>
32.
<td><?=
$objResult
[
"Tel"
];?></td>
33.
</tr>
34.
<tr>
35.
<td>Email</td>
36.
<td><?=
$objResult
[
"Email"
];?></td>
37.
</tr>
38.
</table>
39.
40.
<br>
41.
42.
<table width=
"400"
border=
"1"
>
43.
<tr>
44.
<td width=
"101"
>ProductID</td>
45.
<td width=
"82"
>ProductName</td>
46.
<td width=
"82"
>Price</td>
47.
<td width=
"79"
>Qty</td>
48.
<td width=
"79"
>Total</td>
49.
</tr>
50.
<?
51.
52.
$Total
= 0;
53.
$SumTotal
= 0;
54.
55.
$strSQL2
=
"SELECT * FROM orders_detail WHERE OrderID = '"
.
$_GET
[
"OrderID"
].
"' "
;
56.
$objQuery2
= mysql_query(
$strSQL2
)
or
die
(mysql_error());
57.
58.
while
(
$objResult2
= mysql_fetch_array(
$objQuery2
))
59.
{
60.
$strSQL3
=
"SELECT * FROM product WHERE ProductID = '"
.
$objResult2
[
"ProductID"
].
"' "
;
61.
$objQuery3
= mysql_query(
$strSQL3
)
or
die
(mysql_error());
62.
$objResult3
= mysql_fetch_array(
$objQuery3
);
63.
$Total
=
$objResult2
[
"Qty"
] *
$objResult3
[
"Price"
];
64.
$SumTotal
=
$SumTotal
+
$Total
;
65.
?>
66.
<tr>
67.
<td><?=
$objResult2
[
"ProductID"
];?></td>
68.
<td><?=
$objResult3
[
"ProductName"
];?></td>
69.
<td><?=
$objResult3
[
"Price"
];?></td>
70.
<td><?=
$objResult2
[
"Qty"
];?></td>
71.
<td><?=number_format(
$Total
,2);?></td>
72.
</tr>
73.
<?
74.
}
75.
?>
76.
</table>
77.
Sum Total <?=number_format(
$SumTotal
,2);?>
78.
79.
<?
80.
mysql_close();
81.
?>
82.
</body>
83.
</html>
clear.php
1.
<?
2.
ob_start();
3.
session_start();
4.
session_destroy();
5.
6.
header(
"location:show.php"
);
7.
?>
Screenshot
หน้าจอ Screen แสดงสินค้า เมื่อคลิกที่ Order ระบบก็จะทำการเก็บสินค้าลงใน Cart
หน้าจอ Screen แสดง Cart ที่ได้เลือกไว้
หน้าจอสำหรับ Checkout และกรอกข้อมูลลูกค้า
หน้าจอบันทึกข้อมูลเรียบร้อยแล้ว
หน้าจอแสดงรายละเอียดสั่งซื้อหลังจากเสร็จสิ้นกระบวนการแล้ว
Download Code !!
No comments:
Post a Comment