2014-10-06 48 views
0

我有這個很容易表參加插槽如何生成插入每個日期一行,並增加對每一個成員加入

http://gyazo.com/4ef60f33b16b43884ab64d9db23f18e3

目前這是一個非常簡單的mysqli到HTML表格

基本即時通訊與添加行奮力/爲每個日期列添加

我現在有2個日期

GY azo.com/f6d7020842e1cbafa75f9295340ad49b

我也有一個考勤表

gyazo.com/f88659eef625a14ee41fbe1f4ed30ab2

參加= 1只,意味着該人是在事件

http://gyazo.com/c4bc3fdbc85c642bf45ec173019b7b60

我終究希望它看起來像這樣,並且對於添加的每個成員它都會生成日期相同的列和行

當前代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
     <head> 
       <title>Attendance System</title> 
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
     </head> 
     <body> 

       <h1>Attendance System</h1> 
       <?php 
         // connect to the database 
         include('connect-db.php'); 





         // get the records from the database 
         if ($result = $mysqli->query("SELECT * FROM players ORDER BY CASE WHEN rank = 'Colonel' THEN 0 WHEN rank = 'Lieutenant-Colonel' THEN 1 WHEN rank = 'Major' THEN 2 WHEN rank = 'Captain' THEN 3 WHEN rank = 'Lieutenant' THEN 4 WHEN rank = 'Ensign' THEN 5 WHEN rank = 'Serjeant Major' THEN 6 WHEN rank = 'Colour Serjeant' THEN 7 WHEN rank = 'Serjeant' THEN 8 WHEN rank = 'Corporal' THEN 9 WHEN rank = 'Lance Corporal' THEN 10 WHEN rank = 'Private' THEN 11 WHEN rank = 'Recruit' THEN 12 END, rank")) 
         { 
           // display records if there are records to display 
           if ($result->num_rows > 0) 
           { 
             // display records in a table 
             echo "<table border='1' cellpadding='10'>"; 

             // set table headers 
             echo "<tr><th>ID</th><th>Alias</th><th>Historical Name</th><th>Rank</th><th>Company</th><th>attending</th>"; 

             echo "</tr>"; 
             // close table headers 

             while ($row = $result->fetch_object()) 
             {   


               // set up a row for each record 
               echo "<tr>"; 
               echo "<td>" . $row->id . "</td>"; 
               echo "<td>" . $row->firstname . "</td>"; 
               echo "<td>" . $row->lastname . "</td>"; 
               echo "<td>" . $row->rank . "</td>"; 
               echo "<td>" . $row->company . "</td>"; 
               echo "<td> Attending </td>"; 
               echo "</tr>"; 
             } 

             echo "</table>"; 
           } 
           // if there are no records in the database, display an alert message 
           else 
           { 
             echo "No results to display!"; 
           } 
         } 
         // show an error if there is an issue with the database query 
         else 
         { 
           echo "Error: " . $mysqli->error; 
         } 

         // close database connection 
         $mysqli->close(); 

       ?> 
     </body> 
</html> 
+0

'ORDER BY CASE' - 是我不知道的一些奇特的SQL,或者是'CASE'是你擁有的一列嗎?這可能是一些奇特的SQL;) – 2014-10-06 18:16:31

+0

以及我可能做錯了方式,但它的排名順序:p – user3432493 2014-10-06 18:19:16

+0

@ Fred-ii- https://dev.mysql.com/doc/refman/5.0/en/case。 html – mudasobwa 2014-10-06 18:20:26

回答

0

你嘗試使用N:考勤和日期表米之間的關係?

這將以我最好的方式解決你的問題。您應該創建一個表attendance_has_eventdate(如果您的表以這種方式命名) 此表與您的兩個表都有1:n關係。

我會向你推薦MySQL Workbench。有了這個工具,你可以設計你的數據庫相當不錯,它是免費的。

+0

主要問題是生成表格而不是數據庫本身:P – user3432493 2014-10-06 18:29:39

+0

使用MySQL Workbench,您可以將模型直接導出到數據庫。您還可以導出sql語句以在您的應用程序中使用它 – JSB 2014-10-06 21:35:39

相關問題