แนวทางศึกษา ระบบตะกร้าสินค้า shopping cart อย่างง่าย

เขียนเมื่อ 8 ปีก่อน โดย Ninenik Narkdee
ตะกร้าสินค้า shopping cart array php

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ ตะกร้าสินค้า shopping cart array php

ดูแล้ว 27,751 ครั้ง


บทความต่อไปนี้ จะเป็นการแนะนำแนวทาง การทำระบบตะกร้าสินค้า
หรือ shopping cart เบื้องต้น สามารถนำไปต่อยอดทำระบบที่มีความซับซ้อน
เพิ่มเติมได้  ในที่นี้ เราจะใช้ mysqli แทน mysql ปกติทั้งหมด 
เพื่อเตรียมความพร้อม การยกเลิก mysql ในอนาคตและฝึกการใช้งาน
mysqli ให้คล่องไปในตัว
 
มาใช้ mysqli แทน mysql แบบเดิม ใน php กันอย่างง่าย 
 
สโคปของเนื้อหาทั้งหมด
 
1. เราจะจำลองการสร้างตัวแปร session ของสมาชิกที่ล็อกอิน เพื่อใช้ในการสั่งซื้อ
    สินค้า โดยเมื่อล็อกอินแล้วก็จะไปยังหน้าสมาชิก
2. แสดงรายการสินค้าทั้งหมดให้เลือก เมื่อคลิกไปหน้าสินค้านั้นแล้ว ก็แสดงรายละเอียด
    พร้อมกับปุ่ม "ใส่ตะกร้า"
3. แสดงรายการที่เลือก ในรายการตะกร้าสินค้า สามารถ เปลี่ยนจำนวน พร้อมลบรายการ
    ที่ไม่ต้องการได้
4. เมื่อเลือก checkout หรือตัดสินใจสั้งซื้อรายการในตะกร้าสินค้า ก็จะมีหน้าสรูป รายการ
    พร้อมมีฟอร์มให้กรอกข้อมูลลูกค้าอย่างง่าย รอคำสั่งการบันทึกคำสั่งซื้อ
5. เมื่อทำการบันทึกคำสั่งซื้อ รายการสินค้าใน ตะกร้าสินค้าจะถูกบันทึกลงฐานข้อมูล 
    พร้อมให้ทำการลบรายการในตะกร้าสินค้าออก เพื่อรับการเลือกสินค้าใหม่
6. ในหน้ารายการสั่งซื้อ จะแสดงลิสรายการสั่งซื้อของสมาชิกที่ล็อกอิน อยู่นั้นๆ โดย
    สามารถคลิกที่ view เพื่อดูรายละเอียดรายการสั่งซื้อทั้งหมดได้
 
ไฟล์ทั้งหมดของเราที่จะใช้งาน
 
0. db_connect.php ไฟล์การตั้งค่าการเชิ่อมต่อฐานข้อมูลด้วย mysqli
ดูได้จาก
มาใช้ mysqli แทน mysql แบบเดิม ใน php กันอย่างง่าย 
 
1. login.php ไฟล์จำลองการล็อกอินอย่างง่าย
2. member.php ไฟล์หน้าสมาชิกเบื้องต้น
3. list.php ไฟล์แสดงลิสรายการสินค้าจากฐานข้อมูล
4. view.php ไฟล์แสดงรายละเอียดรายการสินค้านั้นๆ พร้อมปุ่ม "ใส่ตะกร้า"
5. cart.php ไฟล์ตะกร้าสินค้าที่แสดงรายการที่เลือก
6. checkout.php ไฟล์สรูปรายการ และรอรับการบันทึกการสั่งซื้อ
7. order.php ไฟล์ลิสรายการสั่งซื้อที่บันทึกของสมาชิกที่ล็อกอิน
8. view_order.php ไฟล์แสดงรายละเอียดรายการสั่งซื้อที่บันทึก
 
 
ตาราง ฐานข้อมูลสำหรับระบบ ตะกร้าสินค้า
 
 
-- 
-- Table structure for table `tbl_customer`
-- 

CREATE TABLE `tbl_customer` (
  `cus_id` int(11) NOT NULL auto_increment,
  `cus_name` varchar(100) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`cus_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

-- 
-- Dumping data for table `tbl_customer`
-- 

INSERT INTO `tbl_customer` VALUES (1, 'Cus A');
INSERT INTO `tbl_customer` VALUES (2, 'Cus B');
INSERT INTO `tbl_customer` VALUES (3, 'Cus C');
INSERT INTO `tbl_customer` VALUES (4, 'Cus D');
INSERT INTO `tbl_customer` VALUES (5, 'Cus E');

-- --------------------------------------------------------

-- 
-- Table structure for table `tbl_order`
-- 

CREATE TABLE `tbl_order` (
  `order_id` int(11) NOT NULL auto_increment,
  `cus_id` int(11) NOT NULL,
  `total_qty` int(11) NOT NULL,
  `total_price` int(11) NOT NULL,
  `name` varchar(100) collate utf8_unicode_ci NOT NULL,
  `address` varchar(255) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`order_id`),
  KEY `cus_id` (`cus_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

-- 
-- Dumping data for table `tbl_order`
-- 


-- --------------------------------------------------------

-- 
-- Table structure for table `tbl_orderdetail`
-- 

CREATE TABLE `tbl_orderdetail` (
  `orderdetail_id` int(11) NOT NULL auto_increment,
  `order_id` int(11) NOT NULL,
  `pro_id` int(11) NOT NULL,
  `pro_name` varchar(255) collate utf8_unicode_ci NOT NULL,
  `pro_price` int(11) NOT NULL,
  `pro_qty` tinyint(3) NOT NULL,
  `pro_total_price` int(11) NOT NULL,
  PRIMARY KEY  (`orderdetail_id`),
  KEY `order_id` (`order_id`,`pro_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

-- 
-- Dumping data for table `tbl_orderdetail`
-- 


-- --------------------------------------------------------

-- 
-- Table structure for table `tbl_product`
-- 

CREATE TABLE `tbl_product` (
  `pro_id` int(11) NOT NULL auto_increment,
  `pro_name` varchar(100) collate utf8_unicode_ci NOT NULL,
  `pro_price` int(11) NOT NULL,
  PRIMARY KEY  (`pro_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

-- 
-- Dumping data for table `tbl_product`
-- 

INSERT INTO `tbl_product` VALUES (1, 'Pro 111', 100);
INSERT INTO `tbl_product` VALUES (2, 'Pro 222', 200);
INSERT INTO `tbl_product` VALUES (3, 'Pro 333', 100);
INSERT INTO `tbl_product` VALUES (4, 'Pro 444', 250);
INSERT INTO `tbl_product` VALUES (5, 'Pro 555', 400);
 
 
 
ไฟล์ login.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
if(isset($_POST['cus_id'])){  
    // จำลองการล็อกอิน เพื่อกำหนด session สำหรับสมาชิก
    $arr_cus=explode("|",trim($_POST['cus_id']));
    $_SESSION['ses_cus_id']=$arr_cus[0];  
    $_SESSION['ses_cus_name']=$arr_cus[1];  
    header("Location:member.php");  
    exit;  
}  
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>Login Page</h3>
  <br>
  <a href="list.php">List</a> |  <a href="cart.php">Cart</a> |  <a href="member.php">Member</a><br>
   <form id="myform" method="post" action="">  
    <select name="cus_id" id="cus_id">
<?php  
$q="  
SELECT * FROM tbl_customer 
";  
$result = $mysqli->query($q); // ทำการ query คำสั่ง sql   
$total=$result->num_rows;  // นับจำนวนถวที่แสดง ทั้งหมด  
while($rs=$result->fetch_array()){ // วนลูปแสดงข้อมูล  
?>
        <option value="<?=$rs['cus_id']?>|<?=$rs['cus_name']?>"><?=$rs['cus_name']?></option>
<?php   } ?>
    </select>
   <br>
    <input type="submit" name="submit" value="Login">  
    </form>  
</div>           
</body>
</html>


 
 
ไฟล์ member.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
// จำลองการตรวจสอบว่าเป็นสมาชิก ที่ล็อกอินแล้วหรือไม่
if(!isset($_SESSION['ses_cus_id']) || $_SESSION['ses_cus_id']==""){
    header("Location:login.php");  
    exit;      
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Member</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>Member Page {<?=$_SESSION['ses_cus_name']?>}</h3>
  <br>
  <a href="list.php">List</a> |  <a href="cart.php">Cart</a> |  <a href="order.php">Order</a><br>
<?php  
//หน้าแสดงรายละเอียดสมาชิกที่ล็อกอินเบื้องต้น    
$q="  
SELECT * FROM tbl_customer WHERE cus_id='".$_SESSION['ses_cus_id']."'
";  
$result = $mysqli->query($q); // ทำการ query คำสั่ง sql   
$rs=$result->fetch_array();
?>

<table class="table">
<tbody>
<tr>
    <th>Cus ID</th>
    <td><?=$rs['cus_id']?></td>
</tr>
<tr>
    <th>Customer Name</th>
    <td><?=$rs['cus_name']?></td>
</tr>
</tbody>
</table>


</div>           
</body>
</html>


 
 
ไฟล์ list.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>List</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>List Page {<?=$_SESSION['ses_cus_name']?>}</h3>
  <br>
  <a href="login.php">Login</a> |  <a href="cart.php">Cart</a><br>
<table class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Price</th>
        </tr>
    </thead>
<tbody>

<?php  
// วนลูปแสดงรายการอย่างง่าย พร้อมกำหนดลิ้งค์ไปหน้ารายละเอียดสินค้า    
$i=1;
$q="  
SELECT * FROM tbl_product
";  
$result = $mysqli->query($q); // ทำการ query คำสั่ง sql   
$total=$result->num_rows;  // นับจำนวนถวที่แสดง ทั้งหมด  
while($rs=$result->fetch_array()){ // วนลูปแสดงข้อมูล  
?>
<tr>
    <td><?=$i?></td>
    <td>
    <a href="view.php?pro_id=<?=$rs['pro_id']?>">
    <?=$rs['pro_name']?>
        </a>
    </td>
    <td><?=$rs['pro_price']?></td>
</tr>
<?php  $i++;   } ?>
</tbody>
</table>
</div>            
</body>
</html>


 
 
ไฟล์ view.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>List</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>View Page {<?=$_SESSION['ses_cus_name']?>}</h3>
  <br>
  <a href="list.php">List</a> |  <a href="cart.php">Cart</a><br>
<?php  
// แสดงรายละเอียดสินค้านั้น    
$q="  
SELECT * FROM tbl_product WHERE pro_id='".$_GET['pro_id']."'
";  
$result = $mysqli->query($q); // ทำการ query คำสั่ง sql   
$rs=$result->fetch_array();
?>
   <form id="myform" method="post" action="cart.php">  
<table class="table">
<tbody>
<tr>
    <th>ID</th>
    <td><?=$rs['pro_id']?></td>
</tr>
<tr>
    <th>Name</th>
    <td><?=$rs['pro_name']?></td>
</tr>
<tr>
    <th>Price</th>
    <td><?=$rs['pro_price']?></td>
</tr>
<tr>
    <th></th>
    <td>
<!--    ค่าที่ต้องการส่งไปหน้า ตะกร้าสินค้า-->
    <input type="hidden" name="h_pro_id" value="<?=$rs['pro_id']?>">
    <input type="hidden" name="h_pro_price" value="<?=$rs['pro_price']?>">
    <input type="hidden" name="h_pro_name" value="<?=$rs['pro_name']?>">
    
    <input type="submit" name="add_to_cart" value="ใส่ตะกร้า">  
    </td>
</tr>
</tbody>
</table>
    </form>

</div>              
</body>
</html>


 
 
ไฟล์ cart.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
// สวนของการเพิ่มรายการในตะกร้าสินค้า
if(isset($_POST['add_to_cart'])){
    $keyProID=$_POST['h_pro_id'];
    if($_POST['h_pro_id']!="" && $_POST['h_pro_name']!="" && $_POST['h_pro_price']!=""){
        $_SESSION['ses_cart_pro_id'][$keyProID]=$_POST['h_pro_id'];
        $_SESSION['ses_cart_pro_name'][$keyProID]=$_POST['h_pro_name'];
        $_SESSION['ses_cart_pro_qty'][$keyProID][]=1;
        $_SESSION['ses_cart_pro_price'][$keyProID]=$_POST['h_pro_price'];
        $_SESSION['ses_cart_pro_total_price'][$keyProID]=
            array_sum($_SESSION['ses_cart_pro_qty'][$keyProID])*$_SESSION['ses_cart_pro_price'][$keyProID];
    }
}
// ยกเลิก และลบรายการในตัวแปร session
if(isset($_GET['remove']) && $_GET['d_pro_id']!=""){
    $keyProID=$_GET['d_pro_id'];
    unset($_SESSION['ses_cart_pro_id'][$keyProID]);
    unset($_SESSION['ses_cart_pro_name'][$keyProID]);
    unset($_SESSION['ses_cart_pro_qty'][$keyProID]);
    unset($_SESSION['ses_cart_pro_price'][$keyProID]);
    unset($_SESSION['ses_cart_pro_total_price'][$keyProID]);
    header("Location:cart.php");
    exit;
}
// ส่วนของการอัพเดทจำนวนและราคาของแต่ละรายการ เมื่อเปลี่ยนแปลงจำนวน
if(isset($_GET['update']) && $_GET['u_pro_id']!="" && $_GET['new_qty']!="" ){
    $keyProID=$_GET['u_pro_id'];
    unset($_SESSION['ses_cart_pro_qty'][$keyProID]);
    for($i=0;$i<$_GET['new_qty'];$i++){
        $_SESSION['ses_cart_pro_qty'][$keyProID][]=1;
    }
    $_SESSION['ses_cart_pro_total_price'][$keyProID]=
        array_sum($_SESSION['ses_cart_pro_qty'][$keyProID])*$_SESSION['ses_cart_pro_price'][$keyProID];
    header("Location:cart.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Cart</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>Cart Page {<?=$_SESSION['ses_cus_name']?>}</h3>
  <br>
  <a href="list.php">List</a> |  <a href="member.php">Member</a><br>
<table class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Price</th>
            <th>Qty</th>
            <th>Total Price</th>
            <th>Remove</th>
        </tr>
    </thead>
<tbody>

<?php
if(count($_SESSION['ses_cart_pro_id'])>0){
    $i=1;
    foreach($_SESSION['ses_cart_pro_id'] as $k_pro_id=>$v_pro_id){
        $qty_data=array_sum($_SESSION['ses_cart_pro_qty'][$k_pro_id]);
?>
<tr>
    <td><?=$i?></td>
    <td><?=$_SESSION['ses_cart_pro_name'][$k_pro_id]?></td>
    <td><?=$_SESSION['ses_cart_pro_price'][$k_pro_id]?></td>
    <td>
    <select name="qty[]" onchange="window.location='?u_pro_id=<?=$k_pro_id?>&new_qty='+this.value+'&update'">
       <?php for($v=1;$v<=10;$v++){?>
        <option value="<?=$v?>" <?=($qty_data==$v)?"selected":""?> ><?=$v?></option>
        <?php } ?>
    </select>
      </td>
    <td><?=$_SESSION['ses_cart_pro_total_price'][$k_pro_id]?></td>
    <td><a href="?d_pro_id=<?=$k_pro_id?>&remove">Remove</a></td>
</tr>
<?php $i++; } } ?>
<?php
if(count($_SESSION['ses_cart_pro_total_price'])>0){
?>
<tr>
   <td colspan="3"></td>
   <td><?=count($_SESSION['ses_cart_pro_qty'],1)-count($_SESSION['ses_cart_pro_qty'])?></td>
   <td><?=array_sum($_SESSION['ses_cart_pro_total_price'])?></td>
    <td>
    <input type="button" onclick="window.location='checkout.php'" value="Checkout">
    </td>
</tr>
<?php } ?>
</tbody>
</table>

</div>          
</body>
</html>


 
 
ไฟล์ checkout.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
?>
<?php
// บันทึกข้อมูลลงตาราง tbl_order
if(isset($_POST['save_order']) && $_POST['name']!=""){
    $total_qty=count($_SESSION['ses_cart_pro_qty'],1)-count($_SESSION['ses_cart_pro_qty']);
    $total_price=array_sum($_SESSION['ses_cart_pro_total_price']);
    $q="
        INSERT INTO tbl_order (  
           order_id,  
           cus_id,  
           total_qty,  
           total_price,  
           name,  
           address  
        ) VALUES (  
            NULL,
           '".$_SESSION['ses_cus_id']."',  
           '".$total_qty."',  
           '".$total_price."',  
           '".trim(addslashes($_POST['name']))."',  
           '".trim(addslashes($_POST['addresss']))."'  
        )  
    ";
    $mysqli->query($q);
    $order_id=$mysqli->insert_id;

    // วนลูปบันทึกแต่ละรายการลง tbl_orderdetail แล้วลบ session
    if(count($_SESSION['ses_cart_pro_id'])>0){
        foreach($_SESSION['ses_cart_pro_id'] as $k_pro_id=>$v_pro_id){
            $qty_data=array_sum($_SESSION['ses_cart_pro_qty'][$k_pro_id]);
            $q="
                INSERT INTO tbl_orderdetail (  
                    orderdetail_id,
                    order_id,  
                    pro_id,  
                    pro_name,  
                    pro_price,  
                    pro_qty,  
                    pro_total_price  
                ) VALUES (  
                    NULL,
                    '".$order_id."',  
                   '".$k_pro_id."',  
                   '".trim(addslashes($_SESSION['ses_cart_pro_name'][$k_pro_id]))."',  
                   '".$_SESSION['ses_cart_pro_price'][$k_pro_id]."',  
                   '".$qty_data."',  
                   '".$_SESSION['ses_cart_pro_total_price'][$k_pro_id]."'  
                )  
            ";
            $mysqli->query($q);
            
            
            $keyProID=$k_pro_id;
            unset($_SESSION['ses_cart_pro_id'][$keyProID]);
            unset($_SESSION['ses_cart_pro_name'][$keyProID]);
            unset($_SESSION['ses_cart_pro_qty'][$keyProID]);
            unset($_SESSION['ses_cart_pro_price'][$keyProID]);
            unset($_SESSION['ses_cart_pro_total_price'][$keyProID]);            
     } 
    }        
    header("Location:order.php");  
    exit;  
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Checkout</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>Checkout Page {<?=$_SESSION['ses_cus_name']?>}</h3>
  <br>
  <a href="list.php">List</a> |  <a href="member.php">Member</a><br>
<form id="myform" method="post" action="">    
<table class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Price</th>
            <th>Qty</th>
            <th>Total Price</th>
        </tr>
    </thead>
<tbody>

<?php
if(count($_SESSION['ses_cart_pro_id'])>0){
    $i=1;
    foreach($_SESSION['ses_cart_pro_id'] as $k_pro_id=>$v_pro_id){
        $qty_data=array_sum($_SESSION['ses_cart_pro_qty'][$k_pro_id]);
?>
<tr>
    <td><?=$i?></td>
    <td><?=$_SESSION['ses_cart_pro_name'][$k_pro_id]?></td>
    <td><?=$_SESSION['ses_cart_pro_price'][$k_pro_id]?></td>
    <td><?=$qty_data?></td>
    <td><?=$_SESSION['ses_cart_pro_total_price'][$k_pro_id]?></td>
</tr>
<?php $i++; } } ?>
<?php
if(count($_SESSION['ses_cart_pro_total_price'])>0){
?>
<tr>
   <td colspan="3">
       <input type="button" onclick="window.location='cart.php'" value="Edit">
   </td>
   <td><?=count($_SESSION['ses_cart_pro_qty'],1)-count($_SESSION['ses_cart_pro_qty'])?></td>
   <td><?=array_sum($_SESSION['ses_cart_pro_total_price'])?></td>
</tr>
<tr>
    <th>Name:</th>
    <td colspan="4">
    <input type="text" name="name" id="name" size="50">    
    </td>
</tr>
<tr>
    <th>Address:</th>
    <td colspan="4">
    <textarea name="addresss" id="addresss" cols="50" rows="5"></textarea>    
    </td>
</tr>
<tr>
    <th></th>
    <td colspan="4">
      <input type="submit" name="save_order"  value="บันทึกการสั่งซื้อ">  
    </td>
</tr>
<?php } ?>
</tbody>
</table>
    </form>

</div>              
</body>
</html>


 
 
ไฟล์ order.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
// จำลองการตรวจสอบว่าเป็นสมาชิก ที่ล็อกอินแล้วหรือไม่
if(!isset($_SESSION['ses_cus_id']) || $_SESSION['ses_cus_id']==""){
    header("Location:login.php");  
    exit;      
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Order</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>Order Page {<?=$_SESSION['ses_cus_name']?>}</h3>
  <br>
  <a href="list.php">List</a> |  <a href="cart.php">Cart</a> |  <a href="order.php">Order</a><br>
<table class="table">
    <thead>
        <tr>
            <th>Order ID</th>
            <th>Name</th>
            <th>Address</th>
            <th>Qty</th>
            <th>Total Price</th>
            <th>Detail</th>
        </tr>
    </thead>
<tbody>
<?php
// วนลูปแสดงรายการ order ของสมาชิกนั้นๆ ที่กำลังล็อกอิน    
$q="  
SELECT * FROM tbl_order WHERE cus_id='".$_SESSION['ses_cus_id']."'
";      
$result = $mysqli->query($q); // ทำการ query คำสั่ง sql   
$total=$result->num_rows;  // นับจำนวนถวที่แสดง ทั้งหมด  
while($rs=$result->fetch_array()){ // วนลูปแสดงข้อมูล  
?>
<tr>
    <td><?=$rs['order_id']?></td>
    <td><?=$rs['name']?></td>
    <td><?=$rs['address']?></td>
    <td><?=$rs['total_qty']?></td>
    <td><?=$rs['total_price']?></td>
    <td><a href="view_order.php?v_order_id=<?=$rs['order_id']?>">View</a></td>
</tr>
<?php } ?>
</tbody>
</table>


</div>            
</body>
</html>


 
 
ไฟล์ view_order.php
 
<?php
session_start();
include("db_connect.php");
$mysqli=connect(); // เชิ่มต่อฐานข้อมูล
// จำลองการตรวจสอบว่าเป็นสมาชิก ที่ล็อกอินแล้วหรือไม่
if(!isset($_SESSION['ses_cus_id']) || $_SESSION['ses_cus_id']==""){
    header("Location:login.php");  
    exit;      
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>View Order</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:80%;">  
 <h3>View Order Page {<?=$_SESSION['ses_cus_name']?>}</h3>
  <br>
  <a href="list.php">List</a> |  <a href="member.php">Member</a> |  <a href="order.php">Order</a><br>

<table class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Price</th>
            <th>Qty</th>
            <th>Total Price</th>
        </tr>
    </thead>
<tbody>
<?php  
// คิวรี่ข้อมูลตาราง tbl_order    
$q="  
SELECT * FROM tbl_order WHERE order_id='".$_GET['v_order_id']."'
";  
$result = $mysqli->query($q); // ทำการ query คำสั่ง sql   
$rs=$result->fetch_array();
?>

<?php  
// วนลูป คิวรี่ข้อมูลตาราง tbl_orderdetail    
$i=1;
$q2="  
SELECT * FROM tbl_orderdetail WHERE order_id='".$rs['order_id']."'
";  
$result2 = $mysqli->query($q2); // ทำการ query คำสั่ง sql   
$total=$result2->num_rows;  // นับจำนวนถวที่แสดง ทั้งหมด  
while($rs2=$result2->fetch_array()){ // วนลูปแสดงข้อมูล  
?>

<tr>
    <td><?=$i?></td>
    <td><?=$rs2['pro_name']?></td>
    <td><?=$rs2['pro_price']?></td>
    <td><?=$rs2['pro_qty']?></td>
    <td><?=$rs2['pro_total_price']?></td>
</tr>

<?php $i++; } ?>

<tr>
   <td colspan="3">

   </td>
   <td><?=$rs['total_qty']?></td>
   <td><?=$rs['total_price']?></td>
</tr>
<tr>
    <th>Name:</th>
    <td colspan="4">
<?=$rs['name']?>
    </td>
</tr>
<tr>
    <th>Address:</th>
    <td colspan="4">
<?=$rs['address']?>
    </td>
</tr>
<tr>
    <th></th>
    <td colspan="4">
       <input type="button" onclick="window.location='order.php'" value="Back">
    </td>
</tr>

</tbody>
</table>

</div>                   
</body>
</html>



 


   เพิ่มเติมเนื้อหา ครั้งที่ 1 วันที่ 27-06-2017


ไฟล์ที่ include เพื่อติดต่อกับฐานข้อมูล น่าจะเป็นรูปแบบ ฟังก์ชั่น
โหลดได้ที่เนื้อหานี้เลย
สร้าง php function ใช้งาน mysqli เพิ่ม ลบ แก้ไข แสดง ข้อมูล 


กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ







เนื้อหาที่เกี่ยวข้อง






เนื้อหาพิเศษ เฉพาะสำหรับสมาชิก

กรุณาล็อกอิน เพื่ออ่านเนื้อหาบทความ

ยังไม่เป็นสมาชิก

สมาชิกล็อกอิน



( หรือ เข้าใช้งานผ่าน Social Login )




URL สำหรับอ้างอิง





คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )







เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ