Thực hành xây dựng hệ thống giỏ hàng – Shopping Cart (Part 1)

Thảo luận trong 'Training' bắt đầu bởi nguyenhang1112, 28/4/16.

  1. nguyenhang1112

    nguyenhang1112 New Member

    Khóa học PHP Hà Nội
    Ở những bài trước, chúng ta đã đề cập tới các ứng dụng cơ bản, gần gũi với môi trường làm việc của chúng ta. Trong bài này, tôi sẽ tiếp tục hưỡng dẫn các bạn xây dựng hệ thống giỏ hàng (shopping cart) một ứng dụng phổ biến rất thường gặp trên các website cung cấp sản phẩm hiện nay. Nó gần như được thay thế cho việc mua sắm, việc lựa chọn thủ công mà chúng ta vẫn thường làm.
    [​IMG]
    Do nội dung và kiến thức của bài này khá dài, nên tôi sẽ trình bày bài viết này ở 2 phần, để giúp các bạn dễ theo dõi và dễ nắm bắt các kiến thức mà tôi đề cập về ứng dụng.
    Phần 1: Xây dựng trang hiển thị sản phẩm.
    Đầu tiên, ta khởi tạo bảng CSDL đơn giản như sau (xem lại bài 9: cơ bản về ngôn ngữ sql và mysql)
    CREATE TABLE `books` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `title` varchar(255) NOT NULL,
    `author` varchar(100) NOT NULL,
    `price` int(30) NOT NULL,
    PRIMARY KEY (`id`)
    );
    CREATE TABLE `books` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `title` varchar(255) NOT NULL,
    `author` varchar(100) NOT NULL,
    `price` int(30) NOT NULL,
    PRIMARY KEY (`id`)
    );
    Ở trên là một bảng lưu thông tin của các quyển sách với tên, tác giả, giá tiền của các quyển sách.
    INSERT INTO `books` VALUES (1, 'PHP Can Ban', 'Kenny', 115);
    INSERT INTO `books` VALUES (2, 'PHP Nang Cao', 'Kenny', 150);
    INSERT INTO `books` VALUES (3, 'PHP Framework', 'Kenny', 300);
    INSERT INTO `books` VALUES (4, 'Joomla Can Ban', 'Kenny', 100);
    INSERT INTO `books` VALUES (1, 'PHP Can Ban', 'Kenny', 115);
    INSERT INTO `books` VALUES (2, 'PHP Nang Cao', 'Kenny', 150);
    INSERT INTO `books` VALUES (3, 'PHP Framework', 'Kenny', 300);
    INSERT INTO `books` VALUES (4, 'Joomla Can Ban', 'Kenny', 100);
    Tiếp tục ta tạo file style . css để trình bày dữ liệu đẹp mắt
    body
    font:12px verdana;

    . pro
    border:1px solid #999999;
    margin:5px;
    padding:5px;
    width:400px;

    a
    color:#666666;
    text-decoration:none;
    font-weight:900;

    #cart
    border:1px solid #999999;
    margin:5px;
    padding:5px;
    width:400px;
    text-align:center;
    body
    font:12px verdana;

    . pro
    border:1px solid #999999;
    margin:5px;
    padding:5px;
    width:400px;

    a
    color:#666666;
    text-decoration:none;
    font-weight:900;

    #cart
    border:1px solid #999999;
    margin:5px;
    padding:5px;
    width:400px;
    text-align:center;

    Để liệt kê danh sách các quyển sách đang có trong database, ta cần kết nối CSDL với thao tác code như sau. (xem lại bài 10: kết hợp php & mysql trong ứng dụng website).
    <?php
    $connect=mysql_connect("localhost","root","root")
    or die("Can not connect database");
    mysql_select_db("shop",$connect);
    ?>
    <?php
    $connect=mysql_connect("localhost","root","root")
    or die("Can not connect database");
    mysql_select_db("shop",$connect);
    ?>
    Lúc này, ta có thể liệt kê các quyển sách bằng cú pháp sau:
    <?php
    $sql="select * from books order by id desc";
    $query=mysql_query($sql);
    if(mysql_num_rows($query) > 0)

    while($row=mysql_fetch_array($query))

    echo "<div class='pro'>";
    echo "<h3>$row[title]</h3>";
    echo "Tac Gia: $row[author] - Gia: " . number_format($row[price],3)." VND<br />";
    echo "<p align='right'><a href='addcart . php?item=$row[id]'>Mua Sach Nay</a></p>";
    echo "</div>";


    ?>
    <?php
    $sql="select * from books order by id desc";
    $query=mysql_query($sql);
    if(mysql_num_rows($query) > 0)

    while($row=mysql_fetch_array($query))

    echo "<div class='pro'>";
    echo "<h3>$row[title]</h3>";
    echo "Tac Gia: $row[author] - Gia: " . number_format($row[price],3)." VND<br />";
    echo "<p align='right'><a href='addcart . php?item=$row[id]'>Mua Sach Nay</a></p>";
    echo "</div>";


    ?>
    Đoạn code ở trên thực thi việc hiển thị sách nếu trong CSDL ít nhất 1 record. Và chúng sẽ liệt kê tiêu đề sách, tác giả, giá tiền. Ở đây, tôi sử dụng number_format() để lấy ra 3 số 000 cuối, ứng với đơn vị tiền tệ của Việt Nam là VNĐ.
    Còn nếu không, ta phải gán số lượng của chúng là 1.
    Code xử lý hoàn chỉnh trang addcart . php này sẽ như sau:
    <?php
    session_start();
    $id=$_GET['item'];
    if(isset($_SESSION['cart'][$id]))

    $qty = $_SESSION['cart'][$id] + 1;

    else

    $qty=1;

    $_SESSION['cart'][$id]=$qty;
    header("location:cart . php");
    exit();
    ?>
    <?php
    session_start();
    $id=$_GET['item'];
    if(isset($_SESSION['cart'][$id]))

    $qty = $_SESSION['cart'][$id] + 1;

    else

    $qty=1;

    $_SESSION['cart'][$id]=$qty;
    header("location:cart . php");
    exit();
    ?>
    Nôm na, chúng ta có thể hiểu addcart chỉ đơn giản là xử lý số lượng hàng hóa và lưu chúng ở dạng mảng mà thôi.

    <?php
    $ok=1;
    if(isset($_SESSION['cart']))

    foreach($_SESSION['cart'] as $k=>$v)

    if(isset($k))

    $ok=2;



    if ($ok != 2)

    echo '<p>Ban khong co mon hang nao trong gio hang</p>';
    else
    $items = $_SESSION['cart'];
    echo '<p>Ban dang co <a href="cart . php">' . count($items).' mon hang trong gio hang</a></p>';

    ?>
    <?php
    $ok=1;
    if(isset($_SESSION['cart']))

    foreach($_SESSION['cart'] as $k=>$v)

    if(isset($k))

    $ok=2;



    if ($ok != 2)

    echo '<p>Ban khong co mon hang nao trong gio hang</p>';
    else
    $items = $_SESSION['cart'];
    echo '<p>Ban dang co <a href="cart . php">' . count($items).' mon hang trong gio hang</a></p>';

    ?>

    <?php
    session_start();
    ?>
    <html>
    <head>
    <title>Demo Shopping Cart - Created By My Kenny</title>
    <link rel="stylesheet" href="style . css" />
    </head>
    <body>
    <h1>Demo Shopping Cart</h1>
    <div id='cart'>
    <?php
    $ok=1;
    if(isset($_SESSION['cart']))

    foreach($_SESSION['cart'] as $k=>$v)

    if(isset($v))

    $ok=2;



    if ($ok != 2)

    echo '<p>Ban khong co mon hang nao trong gio hang</p>';
    else
    $items = $_SESSION['cart'];
    echo '<p>Ban dang co <a href="cart . php">' . count($items) . ' mon hang trong gio hang</a></p>';

    ?>
    </div>
    <?php
    $connect=mysql_connect("localhost","root","root")
    or die("Can not connect database");
    mysql_select_db("shop",$connect);
    $sql="select * from books order by id desc";
    $query=mysql_query($sql);
    if(mysql_num_rows($query) > 0)

    while($row=mysql_fetch_array($query))

    echo "<div class=pro>";
    echo "<h3>$row[title]</h3>";
    echo "Tac Gia: $row[author] - Gia: ". number_format($row[price],3) . " VND<br />";
    echo "<p align='right'><a href='addcart . php?item=$row[id]'>Mua Sach Nay</a></p>";
    echo "</div>";

    ?>
    </body>
    </html>
    <?php
    session_start();
    ?>
    <html>
    <head>
    <title>Demo Shopping Cart - Created By My Kenny</title>
    <link rel="stylesheet" href="style . css" />
    </head>
    <body>
    <h1>Demo Shopping Cart</h1>
    <div id='cart'>
    <?php
    $ok=1;
    if(isset($_SESSION['cart']))
    foreach($_SESSION['cart'] as $k=>$v)
    if(isset($v))
    $ok=2;
    if ($ok != 2)
    echo '<p>Ban khong co mon hang nao trong gio hang</p>';
    else
    $items = $_SESSION['cart'];
    echo '<p>Ban dang co <a href="cart . php">' . count($items) . ' mon hang trong gio hang</a></p>';
    ?>
    </div>
    <?php
    $connect=mysql_connect("localhost","root","root")
    or die("Can not connect database");
    mysql_select_db("shop",$connect);
    $sql="select * from books order by id desc";
    $query=mysql_query($sql);
    if(mysql_num_rows($query) > 0)
    while($row=mysql_fetch_array($query))
    echo "<div class=pro>";
    echo "<h3>$row[title]</h3>";
    echo "Tac Gia: $row[author] - Gia: " . number_format($row[price],3)." VND<br />";
    echo "<p align='right'><a href='addcart . php?item=$row[id]'>Mua Sach Nay</a></p>";
    echo "</div>";

    ?>
    </body>
    </html>

    Khoa hoc php o ha noi, hoc php o dau la tot?

Chia sẻ trang này