在PHP中从连接表mySql中回显垂直和水平数据

时间:2016-08-12 07:23:40

标签: php mysql

我需要你的帮助。也许这一切都与逻辑有关。所以我的情况是:

我有mySql表,它们是:

记录表

_______________________________________
Student ID  |  Question ID  |  Answer 
_______________________________________
     1      |       1       |    A
     2      |       1       |    C
     3      |       1       |    E
     1      |       2       |    D
     2      |       2       |    B
     3      |       2       |    A
   .......  |   ..........  |  ........
_______________________________________

和学生表:

_________________________________
 Student ID   |    Student Name
_________________________________
       1      |      Ronaldo
       2      |       Messi
       3      |       Neymar
  ..........  |   .............
_________________________________

我想在报告页面中用这样的表格回显它们:

_________________________________________________
 Student    |         Question and Answer
            |____________________________________
            | 1 | 2 | 3 | ... | ... | ... | ... |
_________________________________________________
 Ronaldo    | A | D | ...........................
 Messi      | C | B | ...........................
 Neymar     | E | A | ...........................
 ........   | ...................................
_________________________________________________

我使用PHP,所以我重复使用foreach(){}函数。但是如何在一次查询中垂直和水平回显数据?

对不起我的PHP知识和逻辑错误。谢谢你的关注。

1 个答案:

答案 0 :(得分:0)

一种简单的方法(但绝不是唯一的方法):

首先获取您的用户及其答案:

SELECT 
    s.id AS student_id,
    s.name AS student_name,
    q.question_id,
    q.answer
FROM 
    students AS s
INNER JOIN
    questions AS q
ON
    s.id = q.student_id;

你应该得到这样的行:

array(
    'student_id' => '1',
    'student_name' => 'Ronaldo',
    'question_id' => 1,
    'answer' => 'A',
)

OR

array(
    'student_id' => '2',
    'student_name' => 'Messi',
    'question_id' => 1,
    'answer' => 'C',
),

然后你可以遍历它们来重新组织它们:

$data = array();
foreach($rows as $row){
    if(!isset($data[$row['student_id']])){
        $data[$row['student_id']] = array(
            'student_name' => $row['student_name'],
            'questions' => array(),
        );
    }

    $data[$row['student_id']]['questions'][] => array(
        'question_id' => $row['question_id'],
        'answer' => $row['answer'],
    );
}

这会给你这个数组:

$data = array(
    '1' => array(
        'student_name' => 'Ronaldo',
        'questions' => array(
            array(
                'question_id' => 1,
                'answer' => 'A',
            ),
            array(
                'question_id' => 2,
                'answer' => 'D',
            ),
        ),
    ),
    '2' => array(
        'student_name' => 'Messi',
        'questions' => array(
            array(
                'question_id' => 1,
                'answer' => 'C',
            ),
            array(
                'question_id' => 2,
                'answer' => 'B',
            ),
        ),
    ),  
);

最后,循环遍历最后一个数组,以您希望的方式显示数据:

foreach($data as $student){
    echo $student['student_name'] . ' : ' ;

    foreach($student['questions'] as $question){
        echo 'Question ' . $question['question_id'] . ' : ' . $question['answer'] . "\n";
    }
}

您需要做一些调整以满足您的需求。