By GokiSoft.com| 14:56 18/04/2022|
Học PHP

[Video] Tạo POS bán hàng sử dụng Cookie trong PHP - Khóa học PHP/MySQL - C2110I

Tạo POS bán hàng sử dụng Cookie trong PHP - Khóa học PHP/MySQL


#index.php


<?php
session_start();

$productList = [];
for ($i=0; $i < 16; $i++) {
	$productList[] = [
		'id' => $i,
		'thumbnail' => 'https://i.redd.it/8a0b3zina8i71.jpg',
		'title' => 'San Pham '.$i,
		'price' => 1000 + 100 * $i
	];
}

if(!isset($_SESSION['cartList'])) {
	$_SESSION['cartList'] = [];
}

if(isset($_GET['cart'])) {
	$id = $_GET['cart'];
	$isFind = false;
	for ($i=0; $i < count($_SESSION['cartList']); $i++) {
		if($_SESSION['cartList'][$i]['id'] == $id) {
			$_SESSION['cartList'][$i]['num']++;
			$isFind = true;
			break;
		}
	}

	if(!$isFind) {
		$p = $productList[$id];
		$p['num'] = 1;

		$_SESSION['cartList'][] = $p;
	}
}

//item: id, thumbnail, title, price, num
$cartList = $_SESSION['cartList'];
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>POS Bán Hàng</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<div class="row">
		<div class="col-md-4">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>No</th>
						<th>Thumbnail</th>
						<th>Title</th>
						<th>Price</th>
						<th>Num</th>
						<th>Total Money</th>
					</tr>
				</thead>
				<tbody>
<?php
$index = 0;
$totalMoney = 0;
foreach($cartList as $item) {
	$totalMoney += $item['price'] * $item['num'];

	echo '<tr>
			<td>'.(++$index).'</td>
			<td><img src="'.$item['thumbnail'].'" style="width: 120px;"/></td>
			<td>'.$item['title'].'</td>
			<td>'.$item['price'].'</td>
			<td>'.$item['num'].'</td>
			<td>'.($item['price'] * $item['num']).'</td>
		</tr>';
}
?>
				</tbody>
			</table>
			<div class="form-group">
				<label>Total Money: </label>
				<label><?=$totalMoney?></label>
			</div>
			<div class="form-group">
				<label>Money: </label>
				<input type="text" name="money" class="form-control">
			</div>
			<div class="form-group">
				<label>Cash Back: </label>
				<label>???</label>
			</div>
		</div>
		<div class="col-md-8">
			<div class="row">
<?php
foreach($productList as $item) {
	echo '<div class="col-md-3">
			<img src="'.$item['thumbnail'].'" style="width: 100%" onclick="addCart('.$item['id'].')">
			<p>Price: '.$item['price'].' VND</p>
			<p>'.$item['title'].'</p>
		</div>';
}
?>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	function addCart(id) {
		// console.log(id)
		// ajax -> day lieu xuong Server -> Session
		// js (jquery) -> cookie
		// simple
		window.open('?cart=' + id, '_self')
	}
</script>
</body>
</html>


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó