简体   繁体   English

具有动态行和列的html表的PHP,其中行数据依赖列标题

[英]PHP for html table with dynamic rows and columns where row data relies on column heading

I have created an assessment system that grades each pupil on each lesson. 我创建了一个评估系统,可以对每堂课的每个学生进行评分。 I have a form that creates a table with the following headings: 我有一个表单,用于创建带有以下标题的表:

  • Pupil name 学生名
  • Pupil ID 学生编号
  • Lesson title 课程标题
  • Lesson ID 课程编号
  • Lesson grade 课程成绩

To view the data I am trying to create a table that works a bit like a spreadsheet. 为了查看数据,我试图创建一个工作表有点像电子表格。
Where each row takes the pupil ID and searches in the database for the grade ie 每行获取学生ID,并在数据库中搜索成绩,即

SELECT * FROM table WHERE pupilid = "rowid" AND lessonid = "columnheading"

I can create the column and row headings using PHP with while loops, but I can't figure out how to make each cell link between the column heading and pupil ID. 我可以使用带有while循环的PHP创建列标题和行标题,但是我不知道如何在列标题和瞳孔ID之间建立每个单元格链接。

The only way I have managed to create this is with floating divs. 我设法创建此方法的唯一方法是使用浮动div。
It works but it is very hard to style and the names and grades are in different grids so it is difficult to sort. 它可以工作,但是很难设置样式,名称和等级在不同的网格中,因此很难排序。
I would really appreciate any help / links to ways to do this. 我非常感谢您提供的帮助/链接。
Each week new pupils could be added and each lesson column will be added to the spreadsheet. 每周可以添加新的学生,并且每个课程列都将添加到电子表格中。

The table should sort of look like the one below: 该表应类似于以下内容:

+-------+----+----+--- +
| Pupil | L1 | L2 | L3 |
+-------+----+----+----+
| John  | B  | C+ | D  |
+-------+----+----+----+
| Sarah | B  | A  | F  |
+-------+----+----+----+
|  Jim  | D  | A  | B  |
+-------+----+----+----+

I can create the column/row headings using the code below. 我可以使用以下代码创建列/行标题。 I would really appreciate some help getting the code for the grade bit. 我真的很感谢帮助您获得年级代码的帮助。

<table>
<tr>
    <th>Pupil name</th>
    <?php
        $selectlesson=$connect->query("SELECT DISTINCT lessonid FROM `grades` ");
  while($rowslesson=$selectlesson->fetch_array())
    {
    ?>
    <th><?php echo $rowslesson['lessonid']; ?></th>
    <?php
    }
    ?>

</tr>
<?php
        $selectpupil=$connect->query("SELECT DISTINCT pupilid FROM `grades` ");
  while($rowspupil=$selectpupil->fetch_array())
    {
    ?>
    <tr>
    <td><?php echo $rowspupil['pupilid']; ?></td>
    </tr>
    <?php
    }
    ?>

Thanks in advance for your help. 在此先感谢您的帮助。

Actually I figured out how to do it. 其实我想出了办法。 I repeated the SQL search in the TD and it seemed to work 我在TD上重复了SQL搜索,但似乎可行

<table>
<tr>
    <th>Pupil name</th>
    <?php
        $selectlesson=$connect->query("SELECT DISTINCT lessonid FROM `grades` ");
  while($rowslesson=$selectlesson->fetch_array())
    {
    ?>
    <th><?php echo $rowslesson['lessonid']; ?></th>
    <?php
    }
    ?>

</tr>
<?php
        $selectpupil=$connect->query("SELECT DISTINCT pupilid FROM `grades` ");
  while($rowspupil=$selectpupil->fetch_array())
    {
    ?>
    <tr>
        <td><?php $pupil= $rowspupil['pupilid'];
            echo $rowspupil['pupilid']; ?>
        </td>
        <?php
        $selectlesson=$connect->query("SELECT DISTINCT lessonid FROM `grades` ");
            while($rowslesson=$selectlesson->fetch_array())
                {
                ?>
        <td><?php $lessongrade = $rowslesson['lessonid']; 

            $selectgrade=$connect->query("SELECT * FROM `grades` where lessonid ='$lessongrade' and pupilid = '$pupil' LIMIT 1 ");
            while($rowsgrade=$selectgrade->fetch_array())
            {
            echo    $rowsgrade ['grade'];
            }?>
        </td>
        <?php
        }
        ?>
    </tr>
    <?php
    }
    ?>



</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM