|  | 
	                
  
    | 
	 
        เห็นถามกันบ่อยๆ php เก็บสินค้าลงในตะกร้าลง ด้วย session แบบไม่ให้เลือกสินค้าซ้ำ หรือ เลือกซ้ำแล้วจะให้บวกเพิ่มทีล่ะหนึ่ง     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | เห็นถามกันบ่อยๆ php เก็บสินค้าลงในตะกร้าลง ด้วย session แบบไม่ให้เลือกสินค้าซ้ำ หรือ เลือกซ้ำแล้วจะให้บวกเพิ่มทีล่ะหนึ่ง ไปค้นเอา code เก่า ๆ ที่เขียนไว้ซะเกือบ 5-6 ปีมาให้นำไปดัดแปลงใช้กันดูครับ 
 โครงสร้างตาราง
 
 
  
 
 
CREATE TABLE `product` (
  `ProductID` int(4) NOT NULL auto_increment,
  `ProductName` varchar(100) NOT NULL,
  `Price` double NOT NULL,
  PRIMARY KEY  (`ProductID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
-- 
-- Dumping data for table `product`
-- 
INSERT INTO `product` VALUES (1, 'Product 1', 100);
INSERT INTO `product` VALUES (2, 'Product 2', 200);
INSERT INTO `product` VALUES (3, 'Product 3', 300);
INSERT INTO `product` VALUES (4, 'Product 4', 400);
 
 
 
 Tag : PHP, MySQL
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2012-03-14 14:47:47 | By :
                          webmaster | View :
                          16678 | Reply :
                          25 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | อันนี้แบบเลือกซ้ำ จะให้บวก 1 เข้าไป 
 product.php
 
 <?
//session_start();
//session_destroy();
?><html>
<head>
<title>ThaiCreate.Com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?
mysql_connect("localhost","root","root");
mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM product";
$objQuery = mysql_query($strSQL);
?>
<table width="327"  border="1">
  <tr>
    <td width="101">ProductID</td>
    <td width="82">ProductName</td>
    <td width="79">Price</td>
    <td width="37">Cart</td>
  </tr>
  <?
  while($objResult = mysql_fetch_array($objQuery))
  {
  ?>
  <tr>
    <td><?=$objResult["ProductID"];?></td>
    <td><?=$objResult["ProductName"];?></td>
    <td><?=$objResult["Price"];?></td>
    <td><a href="order.php?ProductID=<?=$objResult["ProductID"];?>">Order</a></td>
  </tr>
  <?
  }
  ?>
</table>
<?
mysql_close();
?>
</body>
</html>
 order.php
 
 <?
ob_start();
session_start();
	
if(!isset($_SESSION["intLine"]))
{
	 $_SESSION["intLine"] = 0;
	 $_SESSION["strProductID"][0] = $_GET["ProductID"];
	 $_SESSION["strQty"][0] = 1;
	 header("location:show.php");
}
else
{
	
	$key = array_search($_GET["ProductID"], $_SESSION["strProductID"]);
	if((string)$key != "")
	{
		 $_SESSION["strQty"][$key] = $_SESSION["strQty"][$key] + 1;
	}
	else
	{
		
		 $_SESSION["intLine"] = $_SESSION["intLine"] + 1;
		 $intNewLine = $_SESSION["intLine"];
		 $_SESSION["strProductID"][$intNewLine] = $_GET["ProductID"];
		 $_SESSION["strQty"][$intNewLine] = 1;
	}
	
	 header("location:show.php");
}
?>
 show.php
 
 <?
session_start();
?>
<html>
<head>
<title>ThaiCreate.Com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?
mysql_connect("localhost","root","root");
mysql_select_db("mydatabase");
?>
<table width="400"  border="1">
  <tr>
    <td width="101">ProductID</td>
    <td width="82">ProductName</td>
    <td width="82">Price</td>
    <td width="79">Qty</td>
    <td width="79">Total</td>
  </tr>
  <?
  $Total = 0;
  $SumTotal = 0;
  for($i=0;$i<=(int)$_SESSION["intLine"];$i++)
  {
	$strSQL = "SELECT * FROM product WHERE ProductID = '".$_SESSION["strProductID"][$i]."' ";
	$objQuery = mysql_query($strSQL);
	$objResult = mysql_fetch_array($objQuery);
	$Total = $_SESSION["strQty"][$i] * $objResult["Price"];
	$SumTotal = $SumTotal + $Total;
  ?>
  <tr>
    <td><?=$_SESSION["strProductID"][$i];?></td>
    <td><?=$objResult["ProductName"];?></td>
    <td><?=$objResult["Price"];?></td>
    <td><?=$_SESSION["strQty"][$i];?></td>
    <td><?=number_format($Total,2);?></td>
  </tr>
  <?
  }
  ?>
</table>
Sum Total <?=number_format($SumTotal,2);?>
<br><br><a href="product.php">Go to Product</a>
</body>
</html>
 Screenshot
 
 
  
 
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-03-14 14:52:45 | By :
                            webmaster |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | อันนี้แบบไม่ให้เลือกซ้ำ แก้ไขที่ไฟล์ order.php 
 order.php
 
 <?
ob_start();
session_start();
	
if(!isset($_SESSION["intLine"]))
{
	 $_SESSION["intLine"] = 0;
	 $_SESSION["strProductID"][0] = $_GET["ProductID"];
	 $_SESSION["strQty"][0] = 1;
	 header("location:show.php");
}
else
{
	
	$key = array_search($_GET["ProductID"], $_SESSION["strProductID"]);
	if((string)$key == "")
	{	
		 $_SESSION["intLine"] = $_SESSION["intLine"] + 1;
		 $intNewLine = $_SESSION["intLine"];
		 $_SESSION["strProductID"][$intNewLine] = $_GET["ProductID"];
		 $_SESSION["strQty"][$intNewLine] = 1;
	}
	
	 header("location:show.php");
}
?>
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-03-14 14:54:54 | By :
                            webmaster |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ทำไม webmaster ไม่ทำเป็นบทความหละฟระ .. เด่วกระทู้ก็ตก เด่วก็หาไม่เจอนะคร้าบ  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-03-14 17:47:27 | By :
                            deawx |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | พอดีมีคนถามด่วน เลยตอบด่วนครับ เดียวจัดเป็นบทความให้ครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-03-14 17:58:57 | By :
                            webmaster |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  บริการดีจริงๆ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-03-14 20:12:58 | By :
                            ikikkok |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-03-14 20:19:17 | By :
                            webmaster |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
 
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Apply จากตัวอย่างครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-11-23 09:20:59 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | อยากรู้เหมือนกันครับ งม แล้วแต่ไม่ได้เลย  อยากทราบ หน้าต่อไปคือต้องรับค่า post แล้ว โค้ด ที่ใช้เปลี่ยนเเปลง up จำนวนสินค้า ต้องเขียนอย่างไรครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-11-30 21:53:26 | By :
                            dekchai |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | แล้วถ้าจากโค้ดชุดนี้ จะให้มันแก้ไขจำนวนสินค้าในตะกร้า ได้ด้วยล่ะคะ ทำไง 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-01-28 12:22:34 | By :
                            Jutamat |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เดียวพรุ่งนี้จะเขียนให้อีกกระทู้ครับ ติดตามได้ครับ  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-01-28 19:57:35 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เราจะเพิ่ม Text Field  เพื่อเพิ่มจำนวนสินค้า ยังไงค่ะ  คือตอนนี้แก้แล้วราคามันไม่คูณตามจำนวนค่ะ 
 อยากรู้เหมือนกับ No.10 เหมือนกันค่ะ
 
 
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-01-28 23:00:24 | By :
                            nanziibaby |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รออย่างใจจดใจจ่อครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-01-29 16:05:20 | By :
                            นิว |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | $_SESSION["intLine"] คือ อะไรครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-07 07:46:58 | By :
                            Blackrabbit |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | จำนวน Line ของ Array Session ที่จัดเก็บครับ  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-07 10:31:21 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รบกวนอยากได้ Comment Code หน่อยอ่ะครับ พอดีกำลังทำระบบนี้เหมือนกันน่าจะดูเป็นเป็นทางได้แต่ยัง งง กับ code อยู่ 
 |  
              | 
 ประวัติการแก้ไข
 2013-12-01 10:20:12
 2013-12-01 10:20:15
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-12-01 09:54:24 | By :
                            cmsarin |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ถ้าเราจะสร้าง attribute เพิ่มมา1ตัว ไว้ที่ตาราง orders ให้ชื่อว่า SumTotal เพื่อเก็บค่าราคาสินค้ารวมของ orderนั้นๆ ผมเลยอยากถามว่าผมต้องเอาตัวแปรอะไรเข้าไปเก็บตอน INSERT INTO orders (OrderDate,Name,Address,Tel,Email,SumTotal) VALUES
 ('".date("Y-m-d H:i:s")."','".$_POST["txtName"]."','".$_POST["txtAddress"]."' ,'".$_POST["txtTel"]."','".$_POST["txtEmail"]."','เอาตัวแปรอะไรมาใส่ครับเพื่อให้ได้ราคาสินค้ารวมของorders')
 
 ";
 (ไฟล์ save_checkout.php)ขอบคุณสำหรับคำตอบล่วงหน้าครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-19 18:09:54 | By :
                            พีรพล ผู้ความหวัง |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณทีมงานมากครับ เป็นประโยชน์มากเลยครับ กำลังพัฒนาต่อ เพื่อฝึกทักษะครับ มีคำถามพวกพีี่ๆว่า จากสคริปนี้ สมมุติว่ามีสินค้า10ชนิด 10 ชิ้น
 หากต้องการดึงรายการในฐานข้อมูลที่ลูกค้าซื้อแล้วมาแสดงว่าของดังกล่าวได้ถูกจำหน่ายไป และเหลืออะไรที่ลูกค้าซื้อได้บ้าง ต้องทำอย่างไรครับ
 
 รบกวนพี่ๆช่วยหน่อยครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2017-03-27 09:17:43 | By :
                            ittiphat |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 |  |