簡體   English   中英

如何使用php / mysql在html表列中獲取層次結構產品的屬性數據?

[英]how to fetch hierarchy product's propertise data in html table columns using php/mysql?

這是我們的(mysql)數據庫結構和數據:

產品:

id  |  title
1   |  samsung galaxy
2   |  iphone

屬性:(請注意:父級屬性具有parent_id = 0)

id  |  title             | parent_id
1   | screen             |    0
2   | screen size        |    1
3   | screen resolution  |    1

產品_屬性:

id  |  products_id   |  propertise_id | value
1   |      1         |       1        |  null
2   |      1         |       2        |  5 inch
3   |      1         |       3        |  500 ppi
4   |      2         |       1        |  null
5   |      2         |       2        |  4.2 inch
6   |      2         |       3        |  330 ppi

所以我們需要這個html輸出:

<table>
   <tr>
       <td> Products : </td>
       <td> samsung galaxy  </td>
       <td> iphone  </td>
   </tr>

   <tr class="parent_Propertise">
       <td> screen : </td>
       <td>  null </td>
       <td>  null </td>
   </tr>

   <tr>
       <td> screen size : </td>
       <td>  5 inch </td>
       <td>  4.2 inch</td>
   </tr>
  <tr>
       <td> screen resolution : </td>
       <td>  500 ppi </td>
       <td>  330 ppi</td>
   </tr>
</table>

我使用此mysql查詢代碼將數據提取到array()中:

 SELECT Products.name, Products_Propertise.value,Propertise.id,Propertise.title,Propertise.parent_id    
    FROM Products_Propertise
    JOIN Propertise ON Products_Propertise.propertise_id=Propertise.id
    JOIN Products ON Products_Propertise.products_id=Products.id
    WHERE  Products_Propertise.products_id=1 OR Products_Propertise.products_id=2 

我用這個遞歸函數在html表中顯示數據:

<?php
//index elements by id
foreach ($resutls as $item) {
        $item['subs'] = array();
        $indexedItems[$item['id']] = (object) $item;
}
//assign to parent
$topLevel = array();
foreach ($indexedItems as $item) {
    if ($item->parent_id == 0) {
        $topLevel[] = $item;
    } else {
        $indexedItems[$item->parent_id]->subs[] = $item;
    }
}
//recursive function
function renderMenu($items) {
     $render = "";
     $i=0;
     foreach ($items as $item) {
        if(!empty($item->subs))
        {
            $i++;
            $render .= "<tr class='parent_Propertise'><td>".$item->title."</td><td></td></tr>";
        }else{
            $render .= "<tr><td>".$item->title."</td><td>".$item->value."</td></tr>";
        }

        if (!empty($item->subs))
            {
                $render .= renderMenu($item->subs);
            }

     }//end foreach 
    return $render;
}

echo "<table><tr><td>Products :<td>";
foreach ($resutls as $item) {
        echo "<td>".$item['name']."<td>";       
}
echo "</tr>";
echo renderMenu($topLevel);
echo "</table>";
?>

但是最終輸出不是我們想要的! 誰能幫我?

一個問題是您沒有從結果中獲取數據

$resutls = mysqli_query($con,$query);

$resutls resutlsmysqli_result 對象 ,而不是數組。 你在做這個

foreach ($resutls as $item) {

相反,您需要的是while循環

while($item = $resutls->fetch_assoc()) {

或程序上

while($item = mysqli_fetch_assoc($resutls)) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM