我将所有数据都存储在数据库的一个表中,我需要像给出的图片一样显示它(excel中的示例)。 数据库中的表格包含所有学生及其导师(图中名为profesors)。所以,我必须展示他们所指导的每一位导师和学生,但就像在示例中一样。
答案 0 :(得分:0)
不幸的是,你没有解释数据库中的数据是如何布局的,所以我需要猜测并根据小说创建示例。
如果您有以下布局的数据:
professor: professor_id | name
student: student_id | name
professor_x_student: professor_id | student_id
...然后你首先用PHP创建二维数组(可读但未经测试的伪意大利面条代码如下,使用你最喜欢的方言/模板/代码风格):
$resultarray = array();
$sth = query("select p.professor_id, s.student_id,
p.name professor_name, s.name student_name from professor p
inner join professor_x_student pxs on p.professor_id = pxs.professor_id
inner join student s on pxs.student_id = s.student_id");
$maxstudents = 0;
while ($row = $sth->fetch_assoc()) {
if (empty($resultarray[$row['professor_id']])) {
$resultarray[$row['professor_id']] = array('id' => $row['id'], 'name' => $row['professor_name'], 'students' => array());
}
$resultarray[$row['professor_id']]['students'][$row['student_id'] = array('id' => $row['student_id'], 'name' => $row['student_name']);
$maxstudents = count($resultarray[$row['professor_id']]['students']) > $maxstudents ? count($resultarray[$row['professor_id']]['students']) : $maxstudents;
}
$resultarray = array_values($resultarray); // Reset array keys!!
foreach ($resultarray as &$professor) {
$professor['students'] = array_values($professor['students']); // Reset array keys !!
}
现在,在您的模板中,您只需要迭代结果并绘制一些嵌套列表:
echo '<ul class="firstlevel">';
foreach ($resultarray as $professor) {
echo '<li>'.$professor['name'];
echo '<ul class="secondlevel">';
foreach ($professor['students'] as $student) {
echo '<li>'.$student['name'].'</li>';
}
echo '</ul>';
echo '</li>';
}
echo '</ul>';
在CSS中它可能应该是这样的:
.firstlevel {
float: left;
}
.secondlevel {
padding-left: 20px;
}
更新:如果你确定你想要使用表,你需要一些附加组件,你需要知道教授的最大学生数是多少(在先前的获取代码中更新,变量$maxstudents
)。此外,您不能使用此方案的关联数组,因此我也改变了该方面的示例。
然后像这样绘制表格:
echo '<table>';
echo '<tr>';
foreach ($resultarray as $professor) {
echo '<td>'.$professor['name'].'</td>';
}
echo '</tr>';
for ($i = 0; $i < $maxstudents; $i++) {
echo '<tr>';
foreach ($resultarray as $professor) {
if (!empty($professor['students'][$i])) {
echo '<td>'.$professor['students'][$i]['name'].'</td>'; // echo n-th student
} else {
echo '<td> </td>'; // empty cell
}
echo '</tr>';
}
echo '</table>';
答案 1 :(得分:0)
// insert all data in a friendly structured array
// order by here determines horizontal and vertical order. May be other if you want.
$rs = $db->query("select profesor, student from table order by profesor, student");
$out = [];
$counter_by_prof = [];
while($row = $rs->fetchAsoc()) {
$counter_by_prof[$row['profesor']]++; // works even on not inicialiced values
$out[$counter_by_prof[$row['profesor']]][$row['profesor']] = $row['student']; // stack by row by profesor
}
// output the table from the array
echo '<table>'; // /tr /td /th (missing later) are optional
// output header row
echo '<tr>';
foreach($counter_by_prof as $prof=>$tot) echo '<th>'.htmlspecialchars($prof);
// output each row
foreach($out as $row){
echo '<tr>';
foreach($counter_by_prof as $prof=>$tot) {
echo '<td>';
if(array_key_exists($prof, $row) echo htmlspecialchars($row[$prof]);
else echo ' ';
}
}
// bonus line: totals
echo '<tr>';
foreach($counter_by_prof as $prof=>$tot) echo '<td>'.$tot;
echo '</table>';
旁注:如果两个教授的名字相同,则需要一个类似的脚本,但面向ID。