dongwang788787 2016-12-31 03:22
浏览 64
已采纳

使用PHP格式化MySQL数据库中多列的结果

I am looking for a way to display data from a MySQL database across multiple columns in a similar fashion to a spreadsheet.

I have not started the table design as yet as I am unsure of the best approach to achieve what I would like.

I have considered the following:

TABLE 1 - Homework Task

id (INT)

task (VARCHAR)

This table is just a list of homework tasks

TABLE 2 - Students

id (INT)

studentName (VARCHAR)

This is just a list of students

TABLE 3 - Homework Grades

id (INT)

homeworkTaskId (INT)

studentId (INT)

grade (VARCHAR)

percentage (DECIMAL)

This will hold the marks for each homework task for each student

Now, what I would like to achieve is to be able to display the data like this:

enter image description here

What I am struggling with is my knowledge of SQL commands and knowing if it is possible to retrieve the data in such a way that I can loop through it to display in this format?

I am using PHP with a PDO connection to the MySQL database.

Many thanks in advance for any possible assistance in this matter.

  • 写回答

2条回答 默认 最新

  • drag2458 2016-12-31 03:53
    关注

    I would use 2 queries, it's much simpler and if your looking it up by the foreign key ( student.id ) in the homework table, it should be fast enough where performance wont be an issue.

       $DB = new PDO('mysql:host='.$conf['dbhost'].';dbname='.$conf['dbname'], $conf['dbuser'], $conf['dbpass']);
    
       $students_stmt =  $DB->query('SELECT * FROM students');
    
       $homework_stmt = $DB->prepare('
           SELECT
                h.id, h.grade, h.percentage, ht.task 
           FROM
                homework AS h
           JOIN
                homework_task AS ht
           ON
              ht.id = t.homeworkTaskId
           WHERE
               h.studentId = :student_id
       ');
    
    
       $data = [];
    
       $max_homework = 0; //maximum number of homework records
    
       while( false !== ( $student = $students_stmt->fetch(PDO::FETCH_ASSOC) )){
    
           $homework_stmt->execute([':studentId'=>$student['id']]);
    
           $i = 1;
           while( false !== ( $homework = $homework_stmt->fetch(PDO::FETCH_ASSOC) )){
              $student["homework_$i"] = $homework;
              ++$i;
           }
           if( $i > $max_homework )  $max_homework = $i;
    
           $data[] = $student;
       }
    

    You'll wind up with an array like this

     $data = [ 0 =>   //first student
         [
            'studentName' => 'Some Guy',
            'homework_1' => [
                'grade' => 'A',
                'percentage' => '92'
            ],
            'homework_2' => [
                'grade' => 'B',
                'percentage' => '85'
            ]
            'homework_3' => [ .... ]
         ], 1 => [ ... //second student ]
      ];
    

    Then you can loop over $data and create the table

    The $max_homework is because when you go to display it you will need to know how many homework columns you need ( [group, percentage] ) that way you can create the proper number of headers in your table and fill the students that have less then that. So each row in the table has the same number of cells, if that makes sense. That is if the number of homework records vary from student to student.

    By the way this code is untested as I have no way to know the full schema of your tables. It's just an example of the simplest approach ( using PDO ).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示