-1

我有 2 列有 6 个项目

column A  column B
a         200
a         300
a         300
b         600
b         200
b         500

当我在我的 html 表中获取列时,我希望它表示如下:

column A column B column C column D
a        200      300      300 
b        600      200      500

使用普通的 php 和 mysql

请问任何1可以给我解决方案吗?

4

1 回答 1

1

1)SELECT colA, colB FROM your_table

2)将数据保存在关联数组中;

   $colAdata=array();
   $colBdata=array();

   foreach ($results as $r) {
     if ($r["colA"]=="a")
          $colAdata[] = $r;
     else
          $colBdata[] = $r;
    }

3) 根据它们对应的 col 打印:

    echo "<table>";

    echo "<tr>";
    echo "<td>A</td>";
    foreach ($colAdata as $r)
           echo "<td>".$r["colB"]."</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>B</td>";
    foreach ($colBdata as $r)
           echo "<td>".$r["colB"]."</td>";

    echo "</tr>";

    echo "</table>";

更新

2)

   foreach ($results as $r) {
          $item = $r["colA"];

          if (!isset($coldata[$item])
              $coldata[ $item ] = $r;
    }

3)

    $possibilities=array_keys($coldata);

    foreach ($possibilities as $p) {
       echo "<tr>";
       echo "<td>$p</td>";
       foreach ($coldata[$p] as $r)
              echo "<td>".$r["colB"]."</td>";
       echo "</tr>";
    }
于 2013-03-30T21:35:37.153 回答