循环遍历嵌套数组以生成不同长度的表

时间:2013-05-10 14:33:34

标签: php sql arrays nested

我试图在不同地点展示不同级别考试的需求。

我有一些代码在页面上输出一系列4个双列表。

每个表格显示第一列中的总注册数和第二列中括号内的总付款额。

每个级别都有一个表格。 每个中心的每个中心都有一行。

E.g。第1栏,最左栏:

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。

另外,我无法在表格中显示每个级别的单个中心。即行

<td class="width8">' . $centre . ' > ' . $level . '</td>

无效。它只显示“Array&gt; A1”,“Array&gt; A2”等。

有什么想法吗?

// 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>';
}

1 个答案:

答案 0 :(得分:0)

也许,这会对你有帮助。

您只需要注意foreach语法:

  

foreach( $ array $ key =&gt; $ 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>';
}