doufan6544 2013-04-09 14:07
浏览 17

从mysql表生成列和行。 数组操作将行显示为列并填充“单元格”

Please, consider the following.

I have a table setup with relationships as follows. A hierarchical structure where the root has a 1:1 relationship with subRoot table, which contains information related to the Root. I can display the table as shown, using a query and a simple while loop.

Simplified model:

Father  1:n  Child  1:n  Root  1:1   subRoot
---          ---         ---         ---
1            S1          P1          yes
1            S1          P2          no
1            S2          P1          no
1            S2          P2          no
1            S3          P1          yes
1            S3          P2          yes
↓            ↓           ↓           ↓

What I am trying to accomplish, is to display the following and I'm having quite a bit of trouble...

Father    Root    S1  S2  S3  →
---       ---     --  --  --
1         P1      yes no  yes
1         P2      no  no  yes
↓

EDIT: The code for the table that I can generate (the first one). This is adapted code as the one i'm working way too long to post here. (sorry if there are any mistakes...)

<?
$q="SELECT * FROM
Child ,
Root,
subRoot
WHERE
Child.Father_ID = 1 AND Child.ID = Root.ID AND Root.ID2 = subRoot.ID2";
$r=mysql_query($q); 
$num=mysql_num_rows($r); 
?>


<table class="jl_tbl" id="hor-minimalist-b"> 
<tr> 
<th width="20px">Child</th> 
<th width="150px">Root</th> 
<th width="3%">subRoot</th> 
</tr>
<?
$i=0; 
while ($i < $num) { 
  $Child=mysql_result($r,$i,"Sx");   
  $Root=mysql_result($r,$i,"Px"); 
  $subRoot=mysql_result($r,$i,"YN");
?> 

<tr> 
<td><? echo $Child; ?></td>
<td><? echo $Root; ?></td>
<td><? echo $subRoot; ?></td>
</tr>
<? 
$i++; 
} 
?>

EDIT for @verbumSapienti .

enter image description here

  • 写回答

1条回答 默认 最新

  • doudou130216 2013-04-09 16:56
    关注
    <?php
        $i=0;
        while ($i < $num)
        {
            $Child=mysql_result($r,$i,"Sx");
            $Root=mysql_result($r,$i,"Px");
            $subRoot=mysql_result($r,$i,"YN");
            $root[$Root] = array($Child => $subRoot);
            $i++;
        }
        echo '<table>';
        echo '<tr>';
        echo '<th>Father</th><th>Root</th>';
        foreach($root as $Root => $array)
        {
            foreach($array as $Child => $subRoot)
            {
                echo "<th>$Child</th>";
            }
        }
        echo '</tr>';
        foreach($root as $Root => $values)
        {
            echo '<tr>'; 
            echo '<td>fatherSource</td>';
            echo "<td>$Root</td>";
            foreach($values as $subRoot)
            {
                echo "<td>$subRoot</td>";
            }
            echo '</tr>';
        }
        echo '</table>';
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)