在表中显示数组数据


Display an array data in a table

我有一个数组,我想在多个表中显示数据。特定年份和学期的所有模块都应在一个表中。例如,在semester_id 1和year 1中找到的所有模块都应在一个单一的表中

Array
(
    [0] => Array
        (
            [year_id] => 1
            [semester_id] => 1
            [module_code] => MATH1103C
            [module_name] => Decision Mathematics
            [module_credit] => 3
        )
    [1] => Array
        (
            [year_id] => 1
            [semester_id] => 1
            [module_code] => ITE1107C
            [module_name] => Language and Communication Seminar
            [module_credit] => 3
        )
    [2] => Array
        (
            [year_id] => 1
            [semester_id] => 1
            [module_code] => MGMT1101C
            [module_name] => Management Seminar
            [module_credit] => 3
        )
    [3] => Array
        (
            [year_id] => 1
            [semester_id] => 1
            [module_code] => HCA1105C
            [module_name] => Computer Architecture
            [module_credit] => 4
        )
    [4] => Array
        (
            [year_id] => 1
            [semester_id] => 1
            [module_code] => PROG1115C
            [module_name] => Object Oriented Software Development I
            [module_credit] => 4
        )
    [5] => Array
        (
            [year_id] => 1
            [semester_id] => 2
            [module_code] => WAT1116C
            [module_name] => Internet Programming I
            [module_credit] => 4
        )
    [6] => Array
        (
            [year_id] => 1
            [semester_id] => 2
            [module_code] => MATH1101C
            [module_name] => Analytic Methods for Computing
            [module_credit] => 4
        )
    [7] => Array
        (
            [year_id] => 1
            [semester_id] => 2
            [module_code] => PROG1116C
            [module_name] => Object Oriented Software Development II
            [module_credit] => 4
        )
    [8] => Array
        (
            [year_id] => 1
            [semester_id] => 2
            [module_code] => DBT1111C
            [module_name] => Database Design
            [module_credit] => 4
        )
    [9] => Array
        (
            [year_id] => 2
            [semester_id] => 1
            [module_code] => PROG2117C
            [module_name] => Desktop Application Development
            [module_credit] => 4
        )
    [10] => Array
        (
            [year_id] => 2
            [semester_id] => 1
            [module_code] => OSS2109C
            [module_name] => Operating Systems
            [module_credit] => 4
        )
    [11] => Array
        (
            [year_id] => 2
            [semester_id] => 1
            [module_code] => CAN2112C
            [module_name] => Network Design & Programming
            [module_credit] => 4
        )
    [12] => Array
        (
            [year_id] => 2
            [semester_id] => 1
            [module_code] => WAT2117C
            [module_name] => Internet Programming II
            [module_credit] => 4
        )
    [13] => Array
        (
            [year_id] => 2
            [semester_id] => 2
            [module_code] => MGMT2104C
            [module_name] => Research & Development Seminar
            [module_credit] => 3
        )
    [14] => Array
        (
            [year_id] => 2
            [semester_id] => 2
            [module_code] => MCT2104C
            [module_name] => Mobile Application Development
            [module_credit] => 4
        )
    [15] => Array
        (
            [year_id] => 2
            [semester_id] => 2
            [module_code] => WAT2124C
            [module_name] => Web Services
            [module_credit] => 4
        )
    [16] => Array
        (
            [year_id] => 2
            [semester_id] => 2
            [module_code] => MATH2323C
            [module_name] => Numerical Methods
            [module_credit] => 4
        )
    [17] => Array
        (
            [year_id] => 2
            [semester_id] => 2
            [module_code] => SDT2114C
            [module_name] => Requirements Engineering
            [module_credit] => 4
        )
    [18] => Array
        (
            [year_id] => 3
            [semester_id] => 1
            [module_code] => SEM3112C
            [module_name] => Project Management Seminar
            [module_credit] => 3
        )
    [19] => Array
        (
            [year_id] => 3
            [semester_id] => 1
            [module_code] => SECU3119C
            [module_name] => Secure Software Development
            [module_credit] => 4
        )
    [20] => Array
        (
            [year_id] => 3
            [semester_id] => 1
            [module_code] => MULT3114C
            [module_name] => Game Development
            [module_credit] => 4
        )
    [21] => Array
        (
            [year_id] => 3
            [semester_id] => 2
            [module_code] => PROJ3105C
            [module_name] => Systems Development Project
            [module_credit] => 9
        )
    [22] => Array
        (
            [year_id] => 3
            [semester_id] => 2
            [module_code] => MGMT3105C
            [module_name] => Entrepreneurship Seminar
            [module_credit] => 3
        )
    [23] => Array
        (
            [year_id] => 3
            [semester_id] => 2
            [module_code] => SEM3113C
            [module_name] => Software Quality Management
            [module_credit] => 4
        )
    [24] => Array
        (
            [year_id] => 3
            [semester_id] => 2
            [module_code] => WAT3125C
            [module_name] => Emerging Web Technologies
            [module_credit] => 4
        )
    [25] => Array
        (
            [year_id] => 3
            [semester_id] => 2
            [module_code] => SDT3104C
            [module_name] => Enterprise Software Development
            [module_credit] => 4
        )
)

这是我到目前为止的代码

if (isset($rows) && is_array($rows)) {
    for ($i = 0; $i < count($rows); $i++) {
        /*echo 'Year ID: ' . $rows[$i]['year_id'] . '<br>';
            echo 'Semester ID : ' . $rows[$i]['semester_id'] . '<br>';
            echo 'Module Code' . $rows[$i]['module_code'] . '<br>';*/
        echo <<<HTML
<div class="title">
        <h1>Year {$rows[$i]['year_id']}</h1>
    </div>
    <div class="datagrid">
        <table>
            <thead>
            <tr>
                <th>Semester {$rows[$i]['semester_id']}</th>
            </tr>
            </thead>
            <thead>
            <tr>
                <th>Module Code</th>
                <th>Module Name</th>
                <th>Credits</th>
                <th>Grade</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>{$rows[$i]['module_code']}</td>
                <td>{$rows[$i]['module_name']}</td>
                <td>{$rows[$i]['module_credit']}</td>
                <td>
                    <select class="combo">
                        <option value=""> --Grade-- </option>
                        <option value="A">A</option>
                        <option value="B">B</option>
                        <option value="C">C</option>
                        <option value="D">D</option>
                        <option value="F">F</option>
                    </select>
                </td>
            </tr>
            <tr class="al">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td bgcolor="#b3ffb3"><b>S.P.A</b></td>
                <td bgcolor="#4dff4d">data</td>
            </tr>
            </tbody>
        </table>

    </div>   
HTML;

您需要将静态代码放在for循环之外(即all)。

您必须仅为来自数据库的数据生成动态行

 ie ({$rows[$i]['module_code']})

不需要使用多个头或表,只需创建一个表,一个头,然后使用内部tr,然后为数组数据创建动态。

 <table>
            <thead>
              <tr>                    
                <th>Module Code</th>  
              </tr>
            </thead>
            <tbody>
           <?php
                 for ($i = 0; $i < count($rows); $i++){
                ?>
              <tr>                    
                <td><?php echo $rows[$i]['module_code'] ?></td>  
              </tr>
               <?php }  ?>
            </tbody>
          </table>

这是为了在需要创建单个表时使用。例如,在semester_id 1和year 1中找到的所有模块都应该在一个表中。在这种情况下,在数组中运行表,并设置上面的条件
如果匹配了新表,则继续使用当前表

在简单查询上迭代

SELECT DISTINCT year_id, semester_id FROM tablename;

对于每一行,打印表头并在另一个准备好的语句的行上迭代

SELECT ... FROM yourQuery WHERE year_id = ? AND semester_id = ?;

打印表格底部,在时结束

结束while(外部迭代)