繁体   English   中英

将mysql和php数据透视表中的html表行合并/合并

[英]combine/merge html table rows into one from mysql and php pivot table

我有一个简单的时间表的mysql表:

    +-------------+--------------+------+-----+-------------------+-----------------------------+
    | Field       | Type         | Null | Key | Default           | Extra                       |
    +-------------+--------------+------+-----+-------------------+-----------------------------+
    | id          | int(11)      | NO   | PRI | NULL              | auto_increment              |      
    | identity    | int(11)      | NO   | MUL | NULL              |
    | start       | datetime     | YES  | MUL | NULL              |                             |
    | end         | datetime     | YES  | MUL | NULL              |    
    +-------------+--------------+------+-----+-------------------+-----------------------------+

我有一个PHP脚本:

$begin = new DateTime( $_SESSION['start'] );
$end = new DateTime( $_SESSION['end'] ); 
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ( $period as $dt )
  $massivequery[]= "case(date(start)) when '".$dt->format( "Y-m-d" )."' then hour(timediff(end,start)) end as '".$dt->format( "Y-m-d" )."'";

$view_start = $_SESSION['start'];
$view_end = $_SESSION['end'];

$result = mysqli_query($con,"
select schedule.identity,".implode(',',$massivequery)." from schedule join names on schedule.id_code=names.id_code where schedule.away is null and schedule.errand is null and date(schedule.start) between '".$view_start."' and '".$view_end."'
");

echo "<table align='center' border='1'>";

$i = 0;
while($row = $result->fetch_assoc())
{
    if ($i == 0) {
      $i++;
      echo "<tr>";
      foreach ($row as $key => $value) {
        echo "<th>" . $key . "</th>";
      }
      echo "</tr>";
    }
    echo "<tr>";
    foreach ($row as $value) {
      echo "<td id='".$row['identity']."'>" . $value . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

问题在于,这为我提供了几乎理想的餐桌。 它是经过透视处理的,所有表标题都是正确的,但问题在于,每个人按计划的每一天都有单独的一行。 我想将具有相同ID的行合并为一行。 我不是说colspan或rowpan。 我得到的桌子

<table align="center" border="1">
<tbody>
<tr><th>identity</th>
<th>2017-03-20</th>
<th>2017-03-21</th>
<th>2017-03-22</th></tr>

<tr><td id="12345">12345</td>
<td id="12345">8</td>
<td id="12345"></td>
<td id="12345"></td></tr>

<tr><td id="12345">12345</td>
<td id="12345"></td>
<td id="12345">8</td>
<td id="12345"></td></tr>

<tr><td id="12345">12345</td>
<td id="12345"></td>
<td id="12345"></td>
<td id="12345">8</td></tr>

</tbody></table>

我想换的桌子

<table align="center" border="1">
<tbody>
<tr><th>identity</th>
<th>2017-03-20</th>
<th>2017-03-21</th>
<th>2017-03-22</th></tr>

<tr><td id="12345">12345</td>
<td id="12345">8</td>
<td id="12345">8</td>
<td id="12345">8</td></tr>

</tbody></table>

这只是带有简单数据的简单示例。 我实际上有大约40-50个不同的身份,通常的长度是30而不是3天。 该查询依赖于另一个应用程序,因此我必须能够使用会话变量来说明时间段。 这就是为什么我无法手动建立适当的mysql查询(或至少我不知道如何)的原因。 再一次,我很确定jquery有某种超级优雅和简单的方法来做到这一点,但是我没看到如何做。

我只需要编辑mysql查询即可使用。 太简单。

我对此进行了编辑(使用分隔符“ +”添加了group_concat()函数):

$massivequery[]= "group_concat(case(date(start)) when '".$dt->format( "Y-m-d" )."' then hour(timediff(end,start)) end SEPARATOR ' + ') as '".$dt->format( "Y-m-d" )."'";

和(添加的分组依据):

select schedule.identity,".implode(',',$massivequery)." from schedule join names on schedule.id_code=names.id_code where schedule.away is null and schedule.errand is null and date(schedule.start) between '".$view_start."' and '".$view_end."' group by schedule.identity

现在我有一个完全动态的数据透视表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM