简体   繁体   English

如何使用php / mysql在html表列中获取层次结构产品的属性数据?

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

this is our (mysql) database structure and data : 这是我们的(mysql)数据库结构和数据:

Products: 产品:

id  |  title
1   |  samsung galaxy
2   |  iphone

Propertise: (note : parent Propertise has parent_id=0) 属性:(请注意:父级属性具有parent_id = 0)

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

Products_Propertise: 产品_属性:

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

so we need this html output : 所以我们需要这个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>

i fetched data in to an array() using this mysql query code : 我使用此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 

and i used this recursive function to show data in html table : 我用这个递归函数在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>";
?>

but final output is not that we want !! 但是最终输出不是我们想要的! can anyone help me? 谁能帮我?

One problem is that you're not fetching the data out of the results 一个问题是您没有从结果中获取数据

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

$resutls is a mysqli_result object , not an array. $resutls resutlsmysqli_result 对象 ,而不是数组。 You're doing this 你在做这个

foreach ($resutls as $item) {

Instead, what you need is a while loop 相反,您需要的是while循环

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

Or procedural 或程序上

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM