遍历嵌套数组以生成不同长度的表 [英] Loop through nested array to generate tables of different lengths
问题描述
我试图显示在不同位置进行不同级别考试的需求.
I'm trying to show demand for exams of different levels in different locations.
我有一些代码可以在一个页面上输出一系列4个两列表.
I have some code that outputs a series of 4 two-column tables across a page.
每个表在第一列中显示注册总数,在第二列中括号中显示支付总额.
Each table shows total registrations in the first column and total payments in brackets in the second column.
每个级别都有一个表格. 每个表在该级别的每个中心都有一行.
There's a table for each level. There's a row in each table for each centre at that level.
例如第一列,最左侧:
A1
______________
______________
Arlington > A1
______________
26 (17)
--------------
El Paso > A1
______________
8 (8)
--------------
White Ridge > A1
________________
0 (0)
----------------
Jonestown > A1
_______________
10 (9)
----------------
Hochberg > A1
_____________
5 (0)
-------------
每个考试级别需要不同的中心,因此我使用了一个嵌套数组.我可以为每个级别显示不同长度的表,但是从我的sql查询中没有获取任何值-一切都为0.
Each exam level requires different centres, so I have used a nested array. I can display tables of varying lengths for each level but am getting no values from my sql queries - just 0 for everything.
此外,我无法在表格中显示每个级别的各个中心.即行
Also, I can't display individual centres for each level in my tables. i.e. the line
<td class="width8">' . $centre . ' > ' . $level . '</td>
不起作用.它只显示"Array> A1","Array> A2"等.
isn't working. It just displays "Array > A1", "Array > A2" etc.
有什么想法吗?
// create array
$levels = array(
"A1" => array(
'Arlington',
'El Paso',
'White Ridge',
'Jonestown',
'Hochberg'
),
"A2" => array(
'Arlington',
'El Paso',
'Hochberg'
),
"B1" => array(
'El Paso',
'White Ridge',
'Jonestown',
'Hochberg'
),
"B2" => array(
'Arlington',
'El Paso',
'White Ridge'
)
);
// Loop through centres and levels
foreach ($levels as $level => $centres) {
echo '<div id="' . $level . '_column">
<h2 class="'.$level.'">'.$level.'</h2>';
foreach ($centres as $centre) {
// Prepare
$stmt1 = $db->prepare("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre=:centre
AND exam.Level=:level");
$stmt2 = $db->prepare("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre=:centre
AND exam.Level=:level");
// Bind
$stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt1->bindParam(':level', $level, PDO::PARAM_STR);
$stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt2->bindParam(':level', $level, PDO::PARAM_STR);
// Execute
$stmt1->execute();
$stmt2->execute();
// Fetch
$row = $stmt1->fetch(PDO::FETCH_ASSOC);
$row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">' . $centre . ' > ' . $level . '</td>
<tr>';
if ($row && $row2) {
foreach ($row as $key => $value) {
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2) {
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
} echo '</div>';
}
推荐答案
也许,这会对您有所帮助.
Maybe, this shall help you.
您只需要注意foreach
语法:
foreach( $ array 为 $ key => $ value )
例如:
$array = array(
"key1" => /*values*/ array(
"value1",
"value2"
),
"key2" => /*values*/ array(
"value1",
"value2"
)
);
foreach($array as $key => $values){
foreach($values as $value){
echo "$key => $value\n";
}
}
会回声:
key1 => value1
key1 => value2
key2 => value1
key2 => value2
因此,您的代码应为:
// Creates arrays
$levels = array(
"A1" => array(
'Arlington',
'El Paso',
'White Ridge',
'Jonestown',
'Hochberg'
),
"A2" => array(
'Arlington',
'El Paso',
'Hochberg'
),
"B1" => array(
'El Paso',
'White Ridge',
'Jonestown',
'Hochberg'
),
"B2" => array(
'Arlington',
'El Paso',
'White Ridge'
)
);
// Loop through centres and levels
foreach ($levels as $level => $centres) {
echo '<div id="' . $level . '_column">
<h2 class="' . $level . '">' . $level . '</h2>';
foreach ($centres as $centre) {
// Prepare
$stmt1 = $db->prepare("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre=:centre
AND exam.Level=:level");
$stmt2 = $db->prepare("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre=:centre
AND exam.Level=:level");
// Bind
$stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt1->bindParam(':level', $level, PDO::PARAM_STR);
$stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt2->bindParam(':level', $level, PDO::PARAM_STR);
// Execute
$stmt1->execute();
$stmt2->execute();
// Fetch
$row = $stmt1->fetch(PDO::FETCH_ASSOC);
$row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">' . $centre . ' > ' . $level . '</td>
<tr>';
if ($row && $row2) {
foreach ($row as $key => $value) {
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2) {
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
} echo '</div>';
}
这篇关于遍历嵌套数组以生成不同长度的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!