PHP列表转换成表格 [英] php list into a table

查看:239
本文介绍了PHP列表转换成表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个问题,昨天和答案,我得到了回答了我的问题的第一部分 - 我现在有一个生成一个类似的列表的查询:

I asked a question yesterday and the answer I got has answered the first part of my problem - I now have a query that generates a list similar to this:

fname, sname, uname, assignment, task, grade
joe, blogs, joe.blogs, 1, 1, 52
joe, blogs, joe.blogs, 1, 2, 58
jim, blogs, jim.blogs, 1, 1, 95
jim, blogs, jim.blogs, 1, 2, 86
amy, blogs, amy.blogs, 1, 1, NULL
amy, blogs, amy.blogs, 1, 2, 76

在等。它采用最新的等级从列表中仍然有效,其中一个学生没有收到一个档次的任务。

an so on. It takes the most recent grade from the list and still works where a student hasn't received a grade for task.

我想接下来就是产生类似于这样一个表:

What I want next is to produce a table that looks similar to this:

      | Assignment 1    | Assignment 2    |
 Name |-----------------|-----------------|
      | Task 1 | Task 2 | Task 1 | Task 2 |
===========================================
Joe.. |  52    |  58    |  ..    |  ..    |
Jim.. |  95    |  86    |  ..    |  ..    |
Amy.. |  U     |  76    |  ..    |  ..    |
...

再次,我已经做过类似的事情之前,但我知道他们是可怕的效率低下 - 理想情况下,我想能够生产此表与阵列单通

again, I've done similar things before, but I know they were horribly inefficient - Ideally I'd like to be able to produce this table with a single pass of the array.

感谢。

推荐答案

你怎么会需要做你迭代通过行同时被添加列。它可能是有效的,以首先执行一个小的查询,选择的列:

What you're gonna need to do is add columns while you're iterating through the rows. It might be efficient to execute a small query first, selecting the columns:

SELECT
  *
FROM assignment_tasks
ORDER BY
  Assignment_ID ASC,
  Task_ID ASC;

然后调整查询加载你所描述的用户,任务,任务命令的阵列。 通过这种方式,在阵列中的值是在相同的顺序在表(逐行,从左至右)。如果值丢失(或NULL,无所谓),打印ü。

Then adapt the query loading the array you described to order by user, assignment, task. This way, the values in the array are in the same order as in the table (row by row, left to right). If a value is missing (or NULL, doesn't matter), print U.

当然,这正好与假设相比条目的阵列中的编号分配和任务的数目是小的。

Of course, this goes with the assumption that the number of assignments and tasks is small compared to the number of entries in the array.

这篇关于PHP列表转换成表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆