0

我需要帮助

假设我有 2 张桌子

表1:产品

 ----------------------------- 
| id |  Code | any else field |
 ----------------------------- 
| 10 | GA 12 | -------------- |
| 11 | GA 1  | -------------- |
 ----------------------------- 

表2:库存

 ---------------------------------------------
| id | product_id |    color    | size | stock |
 ---------------------------------------------
| 1  |   10       | Black       | M    | 12    |
| 2  |   10       | Yellow      | L    | 10    | 
| 3  |   10       | Black       | S    | 12    |
| 4  |   10       | Yellow      | XL   | 10    | 
| 5  |   10       | Red         | S    | 10    | 
| 6  |   10       | Black       | XL   | 12    |
| 7  |   10       | Yellow      | M    | 10    | 
| 8  |   10       | Red         | L    | 12    |
| 9  |   10       | Red         | XL   | 12    |
| 10 |   10       | Gray        | M    | 12    |
 ---------------------------------------------

从表中我们可以看到

product_id 10 有 4 种颜色

Black with size S M and XL
Red with size S L and XL
yellow M L and XL
Gray M

product_id 10 的平均尺寸范围是 S - XL

从那两张表中,我需要在我的网站上得到如下结果

产品和库存详细信息

我尝试使用 php 和 mysql 查询,如下所示:

function getProduct(){
    $Condb = @new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $Condb->connect_errno and die ('Connect Failed : '.$Condb->connect_error);
    $sql = "
        SELECT product.*
        FROM product
        ";
    $res2 = $Condb->query($sql);
    while ($row = $res2->fetch_assoc()){
        $return_data[] = $row;
    }
    $res2->close();
    $Condb->close();            
    $return ['data'] = $return_data;                
    return $return;
}

--

function list_stock($product_id){
    $Condb = @new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $Condb->connect_errno and die ('Connect Failed : '.$Condb->connect_error);

    //Distinct to get size range from product_id
    $sql = "
        SELECT DISTINCT size FROM stock WHERE product_id='$product_id' 
        ORDER BY uk ASC
        ";
    $res2 = $Condb->query($sql);
    $return_size_range = array();
    while ($row = $res2->fetch_assoc()){
        $return_size_range[] = $row['size'];
    }
    $res2->close();

    $return_stock[] = array();
    $return_color = array();
    $sql = "
        SELECT * FROM stock
        WHERE product_id='$product_id'
        GROUP BY product_id, color, size                
        ORDER BY warna, uk ASC
        ";
   $res2 = $Condb->query($sql);
   $n = 0;
   $w = 0;
   while ($row = $res2->fetch_assoc()){
      $size_index = 0;
      if($n%count($return_size_range)== 0){
          $color=$row['color'];
          $return_color[$w]=$color;
          $w=$w+1;
          $n=0;
      }

      while($size_index == 0){
          if($return_size_range[$n] == $row['size']){
              $sz = $row['size'];

              $return_stock[$n]['size' . $color] = $sz;
          $return_stock[$n]['stock' . $color . $sz] = $row['stock'];
              $size_index=1;
          }else{
              $size_index=0;
          }
          $n += 1;
      }
   }
   $res2->close();
   $Condb->close();
   $return ['color'] = $return_color;               
   $return ['size'] = $return_size_range;               
   $return ['stock'] = $return_stock;               
   return $return;
} 

对于 html,我使用以下代码:

<?php
    $product = getProduct();
    $product_detail = $product['data'];
?>
<table>
    <tr>
        <!-- header of table -->
    <tr>
    <?php
    for($p=0 ; $p<count($produk_detail); $p++){
    ?>
    <tr>
        <!-- produk_detail -->
    <tr>
    <tr>
        <td> <!-- with colspan from produk detail column -->
           <?php
               $stock_detail = list_stock($product_detail[$p]['id']);
               $size = $stock_detail['size'];
               $color = $stock_detail['color'];
               $stock = $stock_detail['stock'];
           ?>
           <table>
               <tr>
                   <td>color</td>
                   <?php
                   for($s=0; $s<count($size); $s++){
                       echo '<td>' . $size[$s] . '</td>';
                   }
                   ?>

               </tr>
               <?php
               for($c=0; $c<count($color); $c++){
               ?>
               <tr>
                   <td><?php echo $color[$c]; ?></td>
                   <?php
                   for($st=0; $st<count($size); $st++){
                       echo '<td>';
                       echo isset($stock[$st]['stock' . $color[$c] . $size[st]]) ? $stock[$st]['stock' . $color[$c] . $size[st]] : 0;
                       echo '</td>';
                   }
                   ?>
               </tr>  
               <?php
               }                      
               ?>


               <tr>

               </tr>
           </table>
        </td>
    <tr>

    <?php
    }
    ?>

<table>

根据上面的源代码,我成功地得到了我附加的图像的结果,但是代码很长,并且加载缓慢以获取产品和库存详细信息,

这有可能使我的源代码中的源代码(php)和mysql查询更简单吗?或者这是否可以通过简单的 php 和 mysql 查询获得作为图像附加的结果?

谢谢

4

0 回答 0