PHP / MYSQL:按列填充表

时间:2013-02-11 22:23:32

标签: php mysql database

我的代码目前是:

if (isset($_POST['viewstudentdrop'])) {
    $query = "SELECT students.*, lessons.lessonname 
            FROM lessons INNER JOIN 
            (assignments INNER JOIN students 
            ON assignments.studentid = students.id) 
            ON lessons.lessonid = assignments.lesson
            WHERE (((students.id)=".$_POST['viewstudentdrop']."))";
    $results = $pdodl->query($query);
    while ($row = $results->fetch()) {
            echo "<p>Lesson: ". $row['lessonname'] . "</p>";
    }
   // View Completed Lessons and Times
    $query = "SELECT * 
            FROM studentresponse 
            WHERE studentid = ".$_POST['viewstudentdrop']." 
            ORDER BY lessonsession";
    $results = $pdodl->query($query);
    echo '<table> ';
    while ($row = $results->fetch()) {
    echo '<tr> <td> ' . $row['actiontime'] .'</td>' .
                     ' <td> ' . $row['page'] .'</td>' .
                     ' <td> ' . $row['response'] . '</td>'.
                     ' <td> '. $row['lessonsession'] . '<td> </tr>';
    }
    echo  '</table>';  
}

它产生如下输出:

19:40:44     sda02   C   11360611641    
19:40:46     sda03   D   11360611641    
19:40:50     sda04   3   11360611641    
19:40:53     sda05   A   11360611641    
19:41:22     sda02   B   11360611678    
19:41:24     sda03   C   11360611678    
19:41:31     sda04   5   11360611678    
19:41:34     sda05   B   11360611678    
20:00:39     sda02   B   11360612836    
20:00:41     sda03   C   11360612836    
20:00:44     sda04   3   11360612836    
20:00:47     sda05   B   11360612836    

我可以将上面格式化的数据直接从数据库输出到表格中,但我想将其输出为表格,例如(在课程中删除):

sda02     sda03     sda04     sda05
19:40:44  19:40:46  19:40:50  19:40:53
C         D         3         A

sda02     sda03     sda04     sda05
19:41:22  19:41:24  19:41:31  19:41:34
B         C         5         B

....等下一个课程小组。

之前可能令人困惑。希望这有助于澄清我的问题。谢谢!

2 个答案:

答案 0 :(得分:0)

这是在SQL中执行此操作的一种非常难看的方式。

此答案实现用户定义的变量以为每行生成行号,然后将其用于数据移植到列中。最后,使用带有CASE表达式的聚合函数将数据旋转回列。

select col,
  lessonsession,
  max(case when rn = 1 then value end) Col1,
  max(case when rn = 2 then value end) Col2,
  max(case when rn = 3 then value end) Col3,
  max(case when rn = 4 then value end) Col4
from
(
  select 'actiontime' col, actiontime value, lessonsession, rn
  from 
  (
    select actiontime,
      page, 
      response, 
      lessonsession,
      @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
      @prev:=lessonsession
    from yourtable
    cross join (select @row:=0, @prev:=null) r
    order by lessonsession
  ) s
  union all
  select 'page' col, page value, lessonsession, rn
  from
  (
    select actiontime,
      page, 
      response, 
      lessonsession,
      @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
      @prev:=lessonsession
    from yourtable
    cross join (select @row:=0, @prev:=null) r
    order by lessonsession
  ) s
  union all
  select 'response' col, response value, lessonsession, rn
  from 
  (
    select actiontime,
      page, 
      response, 
      lessonsession,
      @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
      @prev:=lessonsession
    from yourtable
    cross join (select @row:=0, @prev:=null) r
    order by lessonsession
  ) s
) src
group by col, lessonsession
order by lessonsession, col

请参阅SQL Fiddle with Demo

这给出了结果:

|        COL | LESSONSESSION |     COL1 |     COL2 |     COL3 |     COL4 |
--------------------------------------------------------------------------
| actiontime |   11360611641 | 19:40:44 | 19:40:46 | 19:40:50 | 19:40:53 |
|       page |   11360611641 |    sda02 |    sda03 |    sda04 |    sda05 |
|   response |   11360611641 |        C |        D |        3 |        A |
| actiontime |   11360611678 | 19:41:34 | 19:41:31 | 19:41:24 | 19:41:22 |
|       page |   11360611678 |    sda05 |    sda04 |    sda03 |    sda02 |
|   response |   11360611678 |        B |        5 |        C |        B |
| actiontime |   11360612836 | 20:00:39 | 20:00:41 | 20:00:44 | 20:00:47 |
|       page |   11360612836 |    sda02 |    sda03 |    sda04 |    sda05 |
|   response |   11360612836 |        B |        C |        3 |        B |

您可以轻松清理它的一件事是在应用行号后对记录使用临时表。将以下内容插入到临时表中会使代码缩短而不需要重复:

select actiontime,
  page, 
  response, 
  lessonsession,
  @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
  @prev:=lessonsession
from yourtable
cross join (select @row:=0, @prev:=null) r
order by lessonsession

答案 1 :(得分:0)

好的......我已经设法搞砸了我最初使用数组重新组织页面数据的想法。

这是代码(从第二个查询中获取):

    $query = "SELECT * 
            FROM studentresponse 
            WHERE studentid = ".$_POST['viewstudentdrop']." 
            ORDER BY lessonsession";
    $results = $pdodl->query($query);
    while ($row = $results->fetch()) { 
        $iteration[] = array ($row['page'], $row['actiontime'], $row['response'], $row['lessonsession']);
        }
    //Rewrite the data using the array into the format I want
    $x=0; $y=0;
    echo '<table>';
    while (isset($iteration[$x][$y]))  {
        echo '<tr>';        
        while (isset($iteration[$x][$y])) {
            echo '<td>' . $iteration[$x][$y] . '</td>';
            $x++;
        }
        echo '</tr>';
        $x = 0;
        $y++;
}
    echo '</table>';

这会产生以下结果: enter image description here 现在,我所要做的就是打破“课程”中每次变化的表格。

(我现在感觉自己像个程序员 - 我想用分号终止句子。)