使用php旋转html表 [英] pivot html table using php
本文介绍了使用php旋转html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下php代码:
$query_production = "SELECT uploadedby as name, sum(points) as points,
date_format(uploaddate,'%Y-%m-%d') as date FROM imsexport
WHERE uploaddate BETWEEN '2014-01-01 00:00:00' and '2014-01-20 23:59:59'
GROUP BY uploadedby,date";
$result_production = mysql_query($query_production);
$row_production = mysql_fetch_assoc($result_production);
HTML表输出为
name points date
John 147 2014-01-01
Bob 79 2014-01-01
Joe 156 2014-01-01
Sue 116 2014-01-01
John 117 2014-01-02
Bob 186 2014-01-02
Sue 74 2014-01-02
Bob 233 2014-01-03
John 159 2014-01-03
Sue 162 2014-01-03
Bob 162 2014-01-04
Sue 38 2014-01-05
如何使用php旋转此表使其看起来像这样?我已经使用mysql完成此操作,但是代码太长.
How can I pivot this table to look display like this using php? I've already done this using mysql but the code is too long.
Name |2014-01-01|2014-01-02|2014-01-03|2014-01-04|2014-01-05
Bob 79 186 233 162 0
Joe 156 0 0 0 0
John 147 117 159 0 0
Sue 116 74 162 0 38
推荐答案
这将为您提供大部分帮助;我剩下的是如何正确校正左偏移量(或日期/点列对齐;提示,您需要将日期与名称/点对保持一致,并知道它在$cols
中的偏移量).
This will get you most of the way there; what I've left to you is how to get the left offset correct (or the date/points column alignment; hint, you'll need to keep the date with the name/points pair and know it's offset in $cols
).
很显然,您正在处理数据库中的行,因此它会有所不同.
Obviously you're dealing with rows out of a database, so it'll be a little bit different.
请参阅代码下方的键盘演示链接.
See the codepad demo link below the code.
<?php
$data = "John 147 2014-01-01
Bob 79 2014-01-01
Joe 156 2014-01-01
Sue 116 2014-01-01
John 117 2014-01-02
Bob 186 2014-01-02
Sue 74 2014-01-02
Bob 233 2014-01-03
John 159 2014-01-03
Sue 162 2014-01-03
Bob 162 2014-01-04
Sue 38 2014-01-05";
$data = explode("\n", $data);
$cols = array();
$pivot = array();
while ($line = array_shift($data)) {
list($name, $points, $date) = explode(' ', trim($line));
if (!$pivot[$name]) $pivot[$name] = array();
array_push($cols, $date);
array_push($pivot[$name], array('date' => $date, 'points' => $points));
}
$cols = array_unique($cols);
print_r($pivot);
echo implode('|', $cols);
echo PHP_EOL;
foreach ($pivot as $name => $row) {
while ($entry = array_shift($row)) {
echo str_pad($name, 7, ' ') . str_pad($entry['points'], 3, ' ', STR_PAD_LEFT) . '|';
}
echo PHP_EOL;
}
?>
这篇关于使用php旋转html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文