2012-03-05 83 views
1

我有一個半複雜的(不是真的,但它不是最簡單的兩種)數據庫結構:MySQL,PHP和表格數據

appointments = idteacher_idchild_idslot

child = idparent_idnameform

parent = idnamecontact_details

teacher = idnamesubject

slot = idtime

因此,我一直在使用一些多聯接SELECT語句來獲取我所需要的數據,如:

SELECT 
DISTINCT ((c.id +150) *1055) AS reference_number, 
p.name AS parent_name, 
c.name AS child_name, 
c.form AS child_form 
FROM appointments AS a 
INNER JOIN child AS c ON a.child_id = c.id 
INNER JOIN parent AS p ON c.parent_id = p.id 

我想要做的是生成一張表,其中slots是一個軸,而teachers是另一個軸,其中在該時間段內爲該教師預訂的孩子的名稱作爲實際數據。

slot表看起來像這樣:

SQL query: SELECT * FROM `slots` LIMIT 0, 30 ; 
Rows: 30 

id time 
1 2012-03-15 16:00:00 
2 2012-03-15 16:05:00 
3 2012-03-15 16:10:00 
4 2012-03-15 16:15:00 
5 2012-03-15 16:20:00 
6 2012-03-15 16:25:00 
7 2012-03-15 16:30:00 
8 2012-03-15 16:35:00 
9 2012-03-15 16:40:00 
10 2012-03-15 16:45:00 
11 2012-03-15 16:50:00 
12 2012-03-15 16:55:00 
13 2012-03-15 17:00:00 
14 2012-03-15 17:05:00 
15 2012-03-15 17:10:00 
16 2012-03-15 17:15:00 
17 2012-03-15 17:20:00 
18 2012-03-15 17:25:00 

appointments表看起來像這樣幾個條目後:

id teacher_id child_id slot 
1 1 1 3 
2 2 1 5 
3 1 3 2 
4 2 3 6 
5 1 4 4 
6 2 4 7 
7 1 5 1 
8 2 5 9 
9 1 6 8 
10 2 6 3 

我試着用PHP來把東西在一起,但它真的傷了我的頭!

任何人都可以推薦一個解決方案,而不使用外部腳本


編輯按要求這裏是我想要達到一個小的屏幕截圖。我在Excel中這樣做了,但顯然,按照主題,我想在PHP/HTML中使用它。

Tabular Data?


由於提前,

+0

請顯示所需輸出的小例子。 – 2012-03-05 18:27:29

+0

感謝您的建議@MarcusAdams - 我現在已經這麼做了。 – dunc 2012-03-05 21:00:26

回答

1

關閉我的頭頂,我會嘗試這樣的事情。第一位是僞代碼。 這應該會生成一個表,其中包含第一列中的所有教師姓名,然後是每個時間段的列,然後是已預約約會的單元格中的子女姓名。

$appointments =

SELECT DISTINCT ((c.id 150)* 1055)AS REFERENCE_NUMBER, p.name AS PARENT_NAME, c.name AS CHILD_NAME, c.form AS child_form, 一個.id AS任命, a。時隙AS slotID用於 與約會AS一 INNER JOIN孩子爲C ON a.child_id = c.id INNER JOIN父爲P ON c.parent_id = p.id

$teachers = SELECT ID,老師教師

$slots = SELECT ID,時間從插槽

找那些到關聯數組所以你必須$appointments$teachers$slots(你可以添加該代碼)

<?php 

// simply add a blank child element to each slot 
foreach($slots as $slotKey => $slot) 
{ 
    $slots[$slotKey]['child'] = ''; 
} 

// now add the full empty slot list to all teachers 
foreach($teachers as $key => $teacher) 
{ 
    $teachers[$key]['appointments'] = $slots; 
} 

// now to fill in the child names into the teachers slots where they have an appointment 
foreach($teachers as $teacher) 
{ 
    foreach($teacher['slots'] as $slotKey => $slot) 
    { 
     foreach($appointments as $appointment) 
     { 
      if($slot['id'] == $appointment['slotid']) 
      { 
       $teacher[$key][$slotKey]['child'] = $appointment['child_name']; 
      } 
     } 
    } 
} 

// now lets output the HTML table 
?> 

<table> 

    <tr> 
     <th>Teacher</th> 
     <?php foreach($slots as $s): ?> 
      <th><?php echo $s['time']; ?></th> 
     <?php endforeach; ?> 
    </tr> 

    <?php foreach($teachers as $t): ?> 

     <tr> 

      <td><?php echo $t['name']; ?></td> 
      <?php foreach($t['slots'] as $slot): ?> 
       <td><?php echo $slot['child_name']; ?></td> 
      <?php endforeach; ?> 

     </tr> 

    <?php endforeach; ?> 

</table>