15/12/16

[Ứng dụng php] Ứng dụng giỏ hàng bằng PHP và MySQL phần 2

Trong phần 1 mình đã hướng dẫn các bạn tạo bước cơ bản (phần 1) cho việc tạo giỏ hàng với PHP và MySQL với các bước sau:

Bước 1: Mở file index.php và thêm vào:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
 session_start();
 require("includes/connection.php");
 if(isset($_GET['page'])){
 $pages=array("products", "cart");
 if(in_array($_GET['page'], $pages)) {
 $_page=$_GET['page'];
 }else{
 $_page="products";
 }
 }else{
 $_page="products";
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <link rel="stylesheet" href="css/style.css" />
 <link rel="stylesheet" href="css/bootstrap.css" />
 <script type="text/javascript" src="js/bootstrap.min.js"></script>
 <title>Giỏ hàng với PHP và MySQL</title>
</head>
<body>
 <div class="container">
 <div class="row">
 <div class="col-lg-9">
 <?php require($_page.".php"); ?>
 </div>
 <div class="col-lg-3">
 <h1>Giỏ hàng</h1>
 <?php
 if(isset($_SESSION['cart'])){
 $sql="SELECT * FROM products WHERE id_product IN (";
 foreach($_SESSION['cart'] as $id => $value) {
 $sql.=$id.",";
 }
 $sql=substr($sql, 0, -1).") ORDER BY name ASC";
 $query=mysql_query($sql);
 while($row=mysql_fetch_array($query)){
 ?>
 <p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
 <?php
 }
 ?>
 <hr />
 <a href="index.php?page=cart">Đến giỏ hàng</a>
 <?php
 }else{
 echo "<p>Giỏ của bạn trống trơn. Xin vui lòng thêm một số sản phẩm.</p>";
 }
 ?>
 </div>
 </div>
 </div>
</body>
</html>
Trong file này mình sẽ tạo ra list sản phẩm để có thể thêm vào giỏ hàng của mình

Bước 2: Mở file products.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
 if(isset($_GET['action']) && $_GET['action']=="add"){
 $id=intval($_GET['id']);
 if(isset($_SESSION['cart'][$id])){
 $_SESSION['cart'][$id]['quantity']++;
 }else{
 $sql_s="SELECT * FROM products
 WHERE id_product={$id}";
 $query_s=mysql_query($sql_s);
 if(mysql_num_rows($query_s)!=0){
 $row_s=mysql_fetch_array($query_s);
 $_SESSION['cart'][$row_s['id_product']]=array(
 "quantity" => 1,
 "price" => $row_s['price']
 );
 }else{
 $message="Sản phẩm id này là không hợp lệ!";
 }
 }
 }
?>
 <h1>Danh sách sản phẩm</h1>
 <?php
 if(isset($message)){
 echo "<h2>$message</h2>";
 }
 ?>
 <table class="table table-striped">
 <tr>
 <th>Tên</th>
 <th>Mô tả</th>
 <th>Giá</th>
 <th>Thêm giỏ hàng</th>
 </tr>
 <?php
 $sql="SELECT * FROM products ORDER BY name ASC";
 $query=mysql_query($sql);
 while ($row=mysql_fetch_array($query)) {
 ?>
 <tr>
 <td><?php echo $row['name'] ?></td>
 <td><?php echo $row['description'] ?></td>
 <td><?php echo $row['price'] ?> VNĐ</td>
 <td><a href="index.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Thêm giỏ hàng</a></td>
 </tr>
 <?php
 }
 ?>
 </table>
Trong này mình trình bày danh sách của sản phẩm.

Bước 3: Mở file cart.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
?>
<h1>Xem giỏ hàng</h1>
<a href="index.php?page=products">Quay lại trang sản phẩm.</a>
<form method="post" action="index.php?page=cart">
<table class="table table-striped">
<tr>
<th>Tên</th>
<th>Số sản phẩm</th>
<th>Giá</th>
<th>Giá của sản phẩm</th>
</tr>
<?php
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
$totalprice+=$subtotal;
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td>
<td><?php echo $row['price'] ?> VNĐ</td>
<td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>.000 VNĐ</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4">Tổng tiền: <?php echo $totalprice ?></td>
</tr>
</table>
<br />
<button type="submit" name="submit">Cập nhật giỏ hàng</button>
</form>
Chạy chương trình và thu được kết quả:
sp
gsp
Trên đây là hướng dẫn xây dựng giỏ hàng với Php Và MySQL của mình.
Chân thành cảm ơn các bạn theo dõi. Nếu thấy hay xin chia sẻ và like bài viết của mình.
Nguồn: Vnfit.com

0 nhận xét: