[PHP從小白到大牛]-032 PHP-商城項目(八)

後臺購物車信息展現

  • 由於須要計算商品總數和購物車總價, 因此sql語句會比較複雜
$sql = "SELECT {$prefix}cart.id, `{$prefix}user`.username, `{$prefix}user`.id as 'userid', {$prefix}cart.created_at, {$prefix}product.price, {$prefix}cart.product_count, sum({$prefix}product.price*{$prefix}cart.product_count) as 'total_price', sum({$prefix}cart.product_count) as 'total_count' FROM {$prefix}product INNER JOIN {$prefix}cart ON {$prefix}cart.product_id = {$prefix}product.id INNER JOIN `{$prefix}user` ON {$prefix}cart.user_id = `{$prefix}user`.id GROUP BY username";

$cart_result = queryAll($sql);
複製代碼
  • 展現信息
<tbody>
    <?php foreach($cart_result as $cart): ?>
    <tr>
        <td> <?php echo $cart['userid']; ?> </td>
        <td> <?php echo $cart['username']; ?> </td>
        <td> <?php echo $cart['total_count']; ?> </td>
        <td> <?php echo $cart['total_price']; ?> </td>
        <td> <?php echo $cart['created_at']; ?> </td>
        <td> <a href="cart_del.php?id=<?php echo $cart['userid'];?> ">刪除</a> </td>
    </tr>
    <?php endforeach; ?>
</tbody>
複製代碼

前臺增長訂單

D:\php20190701\php_code\0819-24\shop\backend\shop\checkout.phpphp

  • 結算以前, 判斷有沒有登陸, 若是沒有登陸, 跳轉登陸頁面
if (!$current_user) {
	echo "<script>window.location.href = 'login.php';</script>";
}
複製代碼
  • 若是已經登陸
    • 新增訂單數據
    • 更新庫存
    • 刪除購物車數據
// 新增訂單數據
$cart_str = serialize($cart_result);
$created_at = date('Y-m-d H:i:s');
$user_id = $current_user['id'];
$sql = 
<<<TOC INSERT INTO `{$prefix}order`(`price`, `quantity`, `products`, `uid`, `created_at`) VALUES ($total_price, $total_count,'$cart_str',$user_id, '$created_at') TOC;

$res1 = execute($sql);
// 更新庫存
foreach ($cart_result as $key => $cart) {
	$proid = $cart['proid'];
	$product_count = $cart['product_count'];
	$old_stock = intval(queryOne("select stock from {$prefix}product where id = $proid")['stock']);
	$current_stock = $old_stock - intval($product_count);
	$sql = "update {$prefix}product set stock = $current_stock where id = $proid";
	$res2 = execute($sql);
}
// 刪除購物車數據
$sql = "delete from {$prefix}cart where user_id = $user_id";
$res3 = execute($sql);
複製代碼
  • 判斷執行結果, 輸出信息
<?php if($res1 && $res2 && $res3): ?>
<h1 style="color: green;">支付成功!</h1>
<?php else: ?>
<h1 style="color: red;">支付失敗!</h1>
<?php endif;?>
複製代碼

後臺展現訂單

D:\php20190701\php_code\0819-24\shop\backend\admin\orders.phpsql

  • 查詢訂單數據
require_once '../db.func.php';
require_once '../tools.func.php';
$prefix = getDBPrefix();
$sql = "select * from `{$prefix}order` order by created_at desc";
$order_result = queryAll($sql);
foreach ($order_result as $key => $order) {
  $order_result[$key]['username'] = queryOne("select username from {$prefix}user where id = ".$order['uid'])['username'];
}
複製代碼
  • 展現數據
<tbody>
  <?php foreach($order_result as $order):?>
  <tr>
    <td> <?php echo $order['id'] ?> </td>
    <td> <?php echo $order['username'] ?> </td>
    <td> <?php echo $order['price'] ?> </td>
    <td> <?php echo $order['created_at'] ?> </td>
  </tr>
  <?php endforeach;?>
  </tbody>
複製代碼
相關文章
相關標籤/搜索