MySQL,PHP和表格数据

时间:2012-03-05 18:22:40

标签: php mysql tabular

我有一个半复杂的(不是真的,但也不是最简单的)数据库结构:

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?


提前致谢,

1 个答案:

答案 0 :(得分:1)

在我的脑海中,我会尝试这样的事情。第一位是伪代码。 这应该会生成一个表格,其中包含第一列中的所有教师姓名,然后是每个时段的列,然后是预订了约会的单元格中的子名称。

$appointments =

选择  DISTINCT((c.id +150)* 1055)AS reference_number,  p.name AS parent_name,  c.name AS child_name,  c.form AS child_form,  a.id AS任命,  a.slot AS slotid 来自约会AS INNER JOIN child AS c ON a.child_id = c.id. INNER JOIN parent AS p ON c.parent_id = p.id

$teachers = SELECT id,老师FROM老师

$slots = SELECT id,time FROM slots

将这些转换为关联数组,以便$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>