0

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

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

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

每个级别都有一张桌子。该级别的每个中心在每个表中都有一行。

例如,第一列,最左边的列:

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 > A1”、“Array > 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>';
}
4

1 回答 1

0

也许,这会对你有所帮助。

您只需要注意foreach语法:

foreach( $array as $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>';
} 
于 2013-05-13T12:12:03.970 回答