2016-05-02 104 views
0

我有一個數組,我想要在多個表中顯示數據。特定年份和學期的所有模塊應該在一個表中。例如,在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; 
+0

首先根據學期和年份設計另一個陣列,以便您可以輕鬆訪問爲不同的學期和年份繪製不同的表格。 –

+0

它看起來像你的數據來自數據庫。一個簡單的方法是從數據庫讀取過濾的數據並輸出簡單的表格。 –

+0

@Frayne Konok你可以發表一個關於如何從這個人創建另一個數組的答案 – user2650277

回答

0

你需要把靜態代碼外的for循環(即所有)。

您必須只從數據庫

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中找到的所有模塊都應該放在一個表中。在這種情況下,在數組中運行表並將條件放在上面。
如果其他匹配的新表與當前表格繼續

+0

其在這裏並不那麼容易,因爲查詢是一個INNER JOIN幷包含來自不同表格的列 – user2650277

+0

如果您得到正確的數據,那麼您可以輕鬆顯示 –

+0

所有數據數組是動態的,校驗數組。有多個學期和學期 – user2650277

0

遍歷一個簡單的查詢

SELECT DISTINCT year_id, semester_id FROM tablename; 

對於每一行打印表頭和遍歷另一個準備好的聲明

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

的行打印表底部,同時結束

end while while(outer iteration)