gpt4 book ai didi

php - 动态表问题

转载 作者:行者123 更新时间:2023-11-29 00:37:41 25 4
gpt4 key购买 nike

我正在尝试创建一个表,其中的数据表示如下:

Skills   | Project #1 | Project #2 | Project #3
Skill #1 Grade Grade Grade
Skill #2 Grade Grade Grade

本质上,以 Project 开头的列是动态的,通过 SQL 查询获取并存储在数组中。

技能也是动态的,存储在数组中。然后每项技能的成绩应反射(reflect)其所在项目的成绩。

所有这些数据都在数据库中可用。我可以在一次查询中获取技能、项目和成绩。

我正在尝试弄清楚如何让它发挥作用。现在,我只能想办法让技能和项目展示出来。不过,我不知道如何让它们与合适的等级相匹配。这是我的东西

         $sql = "select skills.name as skillName, projects.name, projects_assessments.assessment  from skills
INNER JOIN projects_assessments
ON skills.id = projects_assessments.skillID
INNER JOIN projects
ON projects_assessments.projectID = projects.id
WHERE projects_assessments.studentID = '{student}'
AND skills.teacher = '{teacher}'";
$result = mysql_query($sql) or die (mysql_error());
while($row=mysql_fetch_array($result)) {
$projects[] = $row['name'];
$skills[] = $row['skillName'];
}
echo "<table><tr><th>Skill</th>";
$projects = array_unique($projects);
foreach($projects as $project) {
echo "<th>$project</th>";
}
echo "
</tr>";
foreach($skills as $skill) {
echo "<tr><td>$skill</td></tr>";
}
echo "
</table>

返回:

    Skills   | Project #1 | Project #2 | Project #3
Skill #1
Skill #2

基本上我现在需要的是匹配每项技能的等级。该数据存储在 $row['assessment'] 中。

感谢您的帮助!

最佳答案

只需在二维数组中构建表格:

$table = array();
while ($row = mysql_fetch_array($result)) {
$table[$row['skillName']][$row['name']] = $row['assessment'];
}

$firstRow = current($table);
// draw columns based on $firstRow

foreach ($table as $skillName => $projectList) {
// start row
foreach ($projectList as $assessment) {
// start column with $assessment as value
}
}

重要

确保正确排列行:

ORDER BY skillName, name;

关于php - 动态表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com