2小时我正在寻找我的问题的解决方案,但尚未找到结果。我有一个数组($ array),我使用foreach循环从我的数据库创建。当我使用var_dump($ array)时,我得到了这个:
array(2) {
[a1] => array(4) {
[b1] => string(2) "32"
[b2] => string(2) "98"
[b3] => string(2) "45"
[b4] => string(3) "654"
}
[a2] => array(3) {
[b1] => string(3) "677"
[b2] => string(2) "12"
[b3] => string(3) "430"
}
}
这里重要的是我的数组($ array)存储从数据库中选择并以这种方式存储在$ array中的信息:
$array[a1][b1] = 32;
$array[a1][b2] = 98;
$array[a1][b3] = 45;
$array[a1][b4] = 654;
$array[a2][b1] = 677;
$array[a2][b2] = 12;
$array[a2][b3] = 430;
现在我遇到的问题是如何以这种方式在html表中显示$ array:
Html table wanted:
_____________________
| | a1 | a2 |
|______|______|______|
| b1 | 32 | 677 |
|______|______|______|
| b2 | 98 | 12 |
|______|______|______|
| b3 | 45 | 430 |
|______|______|______|
| b4 | 654 | |
|______|______|______|
另外我需要的另一件事是表行(1,2)和col(1,2,3,4)的值以这种方式存储在数据库中的2个表中:
tableA :
______________
| ID | NAME |
|______|______|
| 1 | a1 |
|______|______|
| 2 | a2 |
|______|______|
tableB :
______________
| ID | NAME |
|______|______|
| 1 | b1 |
|______|______|
| 2 | b2 |
|______|______|
| 3 | b3 |
|______|______|
| 4 | b4 |
|______|______|
如何显示数组:$ array进入html表格,如" Html表格想要" ?
感谢任何能在这个问题上帮助我的人。
答案 0 :(得分:0)
//fetch values for a into associative array
$aValues = fetch_assoc("SELECT * tableA");
//fetch values for b into associative array
$bValues = fetch_assoc("SELECT * tableB");
$htmlTable = '<table>';
//adding header row
$htmlTable .= '<tr><td></td>';
foreach ($aValues as $aValue) {
$htmlTable .= '<td>' . $aValue['name'] . '<td>';
}
$htmlTable .= '</tr>';
//adding other rows, this assumes your $array is already generated
foreach ($bValues as $bValue) {
$htmlTable .= '<tr>';
$htmlTable .= '<td>' . $bValue['name'] . '</td>';
foreach ($aValues as $aValue) {
if (isset($array[$aValue['name']][$bValue['name']])) {
$cellValue = $array[$aValue['name']][$bValue['name']];
} else {
$cellValue = '';
}
$htmlTable .= '<td>' . $cellValue . '<td>';
}
$htmlTable .= '</tr>';
}
$htmlTable .= '</table>';