  | 
              
	              
	                
  
    
	 
        ขอความช่วยเหลือดึงข้อมูลสินค้ามาแล้วแต่จำนวนคงเหลือไม่แสดง     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                ขอความช่วยเหลือดึงข้อมูลสินค้ามาแล้วแต่จำนวนคงเหลือไม่แสดง ฐานข้อมูลที่เก็บเป็นแบบนี้ครับ 
  
 
โดยเมื่อรัน PHP แล้ว จำนวนสินค้าไม่แสดง ดังรูป 
   
 
ไม่รู้ว่า CODE ผิดตรงไหน ช่วยแนะนำด้วยครับ  
 
Code 
<html>
<head>
<title>SunZan-Desgin.Com</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<style>
.number{ text-align : right;}
.number div{
    background: #91F7A4;
    color : #ff0000;
}
#test_report th{ background-color : #21BBD6; color : #ffffff;}
#test_report{
    border-right : 1px solid #eeeeee;
    border-bottom : 1px solid #eeeeee;
}
#test_report td,#test_report th{
    border-top : 1px solid #eeeeee;
    border-left : 1px solid #eeeeee;
    padding : 2px;
}
#txt_year{ width : 70px;}
.fail{ color : red;}
</style>
</head>
<body>
<?php
//เปิดการเชื่อมต่อฐานข้อมูล bpworld_opencart
mysql_connect("localhost","bpworld_root","root");
mysql_select_db("bpworld_opencart");
mysql_query("SET NAMES UTF8");
//ดึงข้อมูลsize
$sizeshow = array();
$strSQL = "SELECT ProductName,size FROM `testproduct` WHERE ProductName='2028' ";
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error());
while($row = mysql_fetch_assoc($qry)){
    $sizeshow[$row['size']] = $row['size'];
}
//ดึงข้อมูลcolor
$allcolor = array();
$strSQL = "SELECT color FROM `testproduct`  WHERE ProductName='2028' ";
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error());
while($row = mysql_fetch_assoc($qry)){
    $allcolor[$row['color']] = $row['color'];
}
//เรียกข้อมูล qty,size,color เพื่อแสดงจำนวน
$qtyshow = array();
$strSQL = "SELECT qty,size,color FROM `testproduct` ";
$strSQL.= "WHERE ProductName='2028' ";
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error());
while($row = mysql_fetch_assoc($qry)){
    $qtyshow[$row['size']][$row['color']] = $row['qty'];
}
echo "<table border='0' id='test_report' cellpadding='0' cellspacing='0'>";
echo '<tr>';//เปิดแถวใหม่ ตาราง HTML
echo '<th>รายชื่อพนักงาน</th>';
//สร้างหัวตารางแสดงสีที่มี
foreach($allcolor as $color=>$color){
    echo '<th width="50">' . $color . '</th>';
}
echo "</tr>";
foreach($sizeshow as $sizecolor=>$psize){
    echo '<tr>';//เปิดแถวใหม่ ตาราง HTML
     
    echo '<td>'. $psize .'</td>';
     
    //เรียกข้อมูลเพื่อแสดง qty,size,color
    foreach($qtyshow as $sizecolor=>$psize){
		
        $qty = isset($qtyshow[$sizecolor][$j]) ? '<div>'.$qtyshow[$sizecolor][$j].'</div>' : 0;
        echo "<td class='number'>", $qty, "</td>";
    }
     
    echo '</tr>';//ปิดแถวตาราง HTML
}
echo "</table>";
mysql_close();//ปิดการเชื่อมต่อฐานข้อมูล
?>
 
 
  Tag : PHP, MySQL               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2017-11-24 07:55:31 | 
                      By :
                          sranuwat | 
                      View :
                          896 | 
                      Reply :
                          4 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 งงเนาะเขียนระบบวนไปวนมา และ ไม่เอา ID มาด้วย 
เอามาเป็นแบบ array แต่ไม่มี ID มาด้วย ใน Code SELECT 
---------------------------------- 
เขียนที่เดียวก็ได้  
SQL :: SELECT * From  testproduct  
LEFT JOIN ............... testproduct.SizeID 
..... 
WHERE ProductName ='$ProductName' 
 
mysql_fetch_assoc  
-------------------------------- 
Size = new table 
Color = new table                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-11-24 08:37:19 | 
                        By :
                            Hararock | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 $strSQL = "SELECT ProductName,size,color,qty FROM `testproduct` WHERE ProductName='2028' "; 
 
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error()); 
 
while($row = mysql_fetch_assoc($qry)){ 
$data[] = $row; 
} 
 
echo '<pre>'; 
var_dump($data); 
echo '</pre>'; 
 
 
ยังงี้มันจะสั่นกว่าไหมครับ เวลาจะเรียกใช้ก็ไปวนเอาออกมากจาก $data [ไม่ได้ลองรัน]                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-11-24 17:00:50 | 
                        By :
                            Fennecfox | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ได้แล้วครับ ขอบคุณทั้ง 2 ท่านเลยนะครับ 
 
  
 
ผมแก้โค๊ดเป็นแบบนี่ครับ 
 
Code 
<html>
<head>
<title>SunZan-Desgin.Com</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<style>
.number{ text-align : center; width:100;}
.number div{
    background: #91F7A4;
    color : #ff0000;
	
}
#test_report th{ background-color : #21BBD6; color : #ffffff;}
#test_report{
    border-right : 1px solid #eeeeee;
    border-bottom : 1px solid #eeeeee;
}
#test_report td,#test_report th{
    border-top : 1px solid #eeeeee;
    border-left : 1px solid #eeeeee;
    padding : 2px;
}
#txt_year{ width : 70px;}
.fail{ color : red;}
</style>
</head>
<body>
<p>
  <?php
//เปิดการเชื่อมต่อฐานข้อมูล bpworld_opencart
mysql_connect("localhost","bpworld_root","root");
mysql_select_db("bpworld_opencart");
mysql_query("SET NAMES UTF8");
//ดึงข้อมูลsize
$sizeshow = array();
$strSQL = "SELECT ProductName,size ,price FROM `testproduct` WHERE ProductName='2028' ORDER BY size ASC ";
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error());
while($row = mysql_fetch_assoc($qry)){
    $sizeshow[$row['size']] = $row['size'];
	
}
//ดึงข้อมูลcolor
$allcolor = array();
$strSQL = "SELECT color FROM `testproduct`  WHERE ProductName='2028' ORDER BY color ASC ";
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error());
while($row = mysql_fetch_assoc($qry)){
    $allcolor[$row['color']] = $row['color'];
}
//เรียกข้อมูล qty,size,color เพื่อแสดงจำนวน
$qtyshow = array();
$strSQL = "SELECT qty,size,color FROM `testproduct` ";
$strSQL.= "WHERE ProductName='2028' GROUP BY  `size` ,  `color`  ";
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error());
while($row = mysql_fetch_assoc($qry)){
    //$qtyshow=[$row['size']=>$row['color']=>$row['qty']];
	
}
echo "<table border='0' id='test_report' cellpadding='0' cellspacing='0'>";
echo '<tr>';//เปิดแถวใหม่ ตาราง HTML
echo '<th>รูปสินค้า</th>';
echo '<th>รุ่น</th>';
echo '<th>ไซร์</th>';
echo '<th>ราคา</th>';
//สร้างหัวตารางแสดงสีที่มี
foreach($allcolor as $color=>$color){
    echo '<th width="50" align="center">' . $color . '</th>';
}
echo "</tr>";
 echo '<tr>';//เปิดแถวใหม่ ตาราง HTML
echo '<td width="150" align="center" rowspan="3"> </td>';
foreach($sizeshow as $sizecolor=>$psize){
   	
    echo '<td width="150" align="center">2028</td>';
    echo '<td width="150" align="center">'. $psize .'</td>';
	    
    //เรียกข้อมูลเพื่อแสดง qty,size,color
	//ดึงข้อมูลcolor
$allsize = array();
$priceshow = array();
$strSQL = "SELECT qty,price FROM `testproduct`  WHERE ProductName='2028' AND size='".$psize."' GROUP BY size,color ORDER BY color ASC";
$qry = mysql_query($strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysql_error());
while($row = mysql_fetch_assoc($qry)){
    $allsize[$row['qty']] = $row['qty'];
	$priceshow[$row['price']] = $row['price'];
}
    //show price
	foreach($priceshow as $sizeprice=>$pprice){
		echo '<td width="100" align="center">'. number_format($pprice,0) .'</td>';
		
	}
    foreach($allsize as $color2=>$color2){
		if($color2==''){$sqty='0';} else {$sqty=$color2;}
	echo "<td class='number' >", $sqty , "</td>";
}
     
    echo '</tr>';//ปิดแถวตาราง HTML
}
echo "</table>";
mysql_close();//ปิดการเชื่อมต่อฐานข้อมูล
?>
</p>
<p>  </p>
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-11-24 17:26:53 | 
                        By :
                            sranuwat | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แล้วสี Black Green Pink คุณแค่ SELECT เอามาเฉยๆ คุณจะไม่ Sum(QTY) บ้างหรอ? จะได้รู้ จำนวนที่แท้ จริง 
--------------------------- 
และ Code mysql SELECT ProductName='2028' 
-------------------------- 
ถ้า มีรายการ 2029 เกิดขื้นล่ะ 
-------------------------- 
ลองไปคิดดูครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-11-25 08:23:41 | 
                        By :
                            Hararock | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |