表结构php矩阵网格

时间:2018-08-14 10:25:20

标签: php html

我想使用数据库中的宽度和长度来创建矩阵网格。 价格需要显示在关联的宽度和放置单元格中。

例如:

width x drop:  600 700 800 900   
         600   226
         700       236
         800   248
         900           290

由于尚未创建产品实例,因此某些值将不存在。

我有三个表:

 1) product_instance (id, product_name, width_id, drop_id, price)
 2) product_width (id, width)
 3) product drop (id, drop)

这是我到目前为止所拥有的:

<table>
   <thead>
     <tr>
       <th>w x d</th>

        <?
         $query = "SELECT * FROM product_instance
                INNER JOIN product_width ON product_instance.width_id 
                = product_width.width_id 
                INNER JOIN product_drop ON product_instance.drop_id = 
                product_drop.drop_id
                INNER JOIN products ON product_instance.product_id = 
                products.product_id WHERE product_instance.product_id 
                = 1";

       $select_products = mysqli_query($connection, $query);
       while ($row = mysqli_fetch_assoc($select_products)) {
          $width_value = $row['width_value'];
            echo "<th>$width_value</th>";
          }
         ?>
         </tr>
         </thead>

         <tbody>

         <?

          $query = "SELECT DISTINCT * FROM product_instance
                   INNER JOIN product_width ON 
                    product_instance.width_id 
                   = product_width.width_id 
                INNER JOIN product_drop ON product_instance.drop_id = 
               product_drop.drop_id
                INNER JOIN products ON product_instance.product_id = 
                products.product_id WHERE product_instance.product_id 
                = 1";

             $select_products = mysqli_query($connection, $query);
              while ($row = mysqli_fetch_assoc($select_products)) {

               $drop_value = $row['drop_value'];
                 $price = $row['price'];

                                    echo "<tr>";
                                    echo "<td>$drop_value</td>";
                                    echo "<td>$price</td>";
                                    echo "</tr>";
                                }
                                ?>


 </tbody>
 </table>

我不确定该怎么做,已经环顾四周,但找不到解决方法。

1 个答案:

答案 0 :(得分:0)

我设法使用PHPivot

找到了可行的解决方案

我的代码现在看起来像这样:

<?php

 require 'pivot/PHPivot.php';

//Get the data
//Could be from a database, JSON, etc

 $query = "SELECT * FROM product_instance
           INNER JOIN product_width ON product_instance.width_id = 
           product_width.width_id 
           INNER JOIN product_drop ON product_instance.drop_id = 
           product_drop.drop_id
           INNER JOIN products ON product_instance.product_id = 
            products.product_id WHERE product_instance.product_id = 1";

         $select_products = mysqli_query($connection, $query);
                            $result = [];
                            $counter = 0;

        while ($row = mysqli_fetch_array($select_products)) {

               $menu[$counter] = array(
               "width" => $row['width_value'],
               "price" => $row['price'],
                "drop" => $row['drop_value']
               );
              array_push($result,$menu[$counter]);
              $counter ++;
             }


$data = $result;
//Just needs to be saved in an associative PHP array   

$products = PHPivot::create($data)
             ->setPivotRowFields('width')
             ->setPivotColumnFields('drop')                                  
             ->setPivotValueFields('price',PHPivot::PIVOT_VALUE_SUM, 
             PHPivot::DISPLAY_AS_VALUE, 'Drop')
             ->addFilter('width', '', PHPivot::COMPARE_NOT_EQUAL)
             //Filter out blanks/unknown genre
             ->generate();

 echo $products->toHtml();

?>