PHP MySQL结果集

时间:2015-05-30 15:09:07

标签: php mysql html-table

我绝对不知道如何安排结果集。

我有一张目前严格包含日期的表格。

DATES   
ID  Dates
1   2015-05-26
2   2015-05-27
3   2015-05-28
4   2015-05-29
5   2015-05-30

这些日期中的每一个也都是当天的表格名称,用于跟踪用于培训的用户/代理商。

2015-05-26
ID  Attendance
101 Present
201 N/A
301 Present
401 Present
501 Present

最终目标是创建下表:

enter image description here

现在,这是我为表头完成的工作。

$data_date = true;

$list = "SELECT * FROM list ORDER BY id DESC LIMIT 5";
$result = mysql_query($list);
while ($row = mysql_fetch_assoc($latest)){
    $dates[] = $row['date']; 
}
foreach($dates as $x){
    $data_date .= "<th>$x</th>";
} 

通过简单地回显$ data_date,我可以显示如下的标题。

<table>
<tr>
<th>AGT</th>
<?php echo $data_date; ?>
</tr>
<tr>
</tr>
</table>

enter image description here

到目前为止一切顺利。获取数组并为每个数组创建新行并从日期数组中查询每一天的最后一步让我感到茫然。

我已经尝试了一个多小时,似乎无法围绕查询每个代理的新行所需的数组。

任何指针或帮助肯定会受到赞赏。

1 个答案:

答案 0 :(得分:1)

或许这是一个开始。日期 - 数学例程可以获得5天的出勤结果

考虑以下

create table agent
(
agentid int NOT NULL PRIMARY KEY,   -- use AUTO_INCREMENT, whatever
agentname varchar(60)
);

insert agent (agentid,agentname) values (109,'Guy Smiley');
insert agent (agentid,agentname) values (110,'Susie Chapstick');
-- select * from agent

create table class_session
(   -- a session is say Monday to Friday, captures a start date
sessionid int not null PRIMARY KEY, -- the week session #
startdate datetime not null,
location varchar(40) -- etc etc
);

insert class_session (sessionid,startdate,location) values (1001,'10/1/2014','Ritz London');

create table session_signup
(   -- the list of agents who should be present
sessionid int not null, -- the week session #
agentid int not null
);


create table agent_present
(
sessionid int not null, -- the week session #
agentid int NOT NULL,
thedate datetime not null,
thestatus varchar(40) -- present, present but sleeping, etc
);