我有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给我解决方案吗?
答案 0 :(得分: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>";
}