1

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

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

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

这些技能也是动态的并存储在数组中。然后每个技能的成绩应该反映它所在项目的成绩。

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

我试图弄清楚如何使这项工作。现在,我只能弄清楚如何让技能和项目展示出来。不过,我不知道如何使它们与适当的等级相匹配。这就是我所拥有的

         $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'] 中。

谢谢你的帮助!

4

1 回答 1

1

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

$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;
于 2012-11-29T06:42:57.930 回答