我有这个代码,它将生成一个多年的表格,从2006年到2017年分成每组5年......
如何将标题置于表格顶部,如下例所示。
<?php
$chunkSize = 5;
$starting_year = 2006;
$ending_year = date("Y");
//create an array of years
$years = range($starting_year,$ending_year);
//[2006,2007,....,2016,2017]
//split years in required size
$chunked = array_chunk($years,$chunkSize);
//[ [2006,....,2010], [2011,...2015], [2016,2017]]
//reverse it
$reversed = array_reverse($chunked);
//[ [2016,2017], [2011,...2015], [2006,....,2010]]
foreach($reversed as $reverse) {
echo "<b>Year $year</b>";
echo "<table class='table table-striped table-bordered'><tbody>";
echo "<tr>";
foreach($reverse as $year) {
echo "<th>{$year}</th>";
}
echo "</tr><tr>";
foreach($reverse as $year) {
$result= $myDB->query("SELECT total FROM ".$myDB->prefix("statistics")." WHERE year='{$year}'") or die(mysql_error());
echo "<td>{$result['total']}</td>";
}
echo "</tr>";
echo "</tbody></table>";
}
当前输出
**Year 2017**
2016 2017
total total
**Year 2017**
2011 2012 2013 2014 2015
total total total total total
**Year 2015**
2006 2007 2008 2009 2010
total total total total total
期望的输出
**Year 2016-2017**
2016 2017
total total
**Year 2011 - 2015**
2011 2012 2013 2014 2015
total total total total total
**Year 2006 - 2010**
2006 2007 2008 2009 2010
total total total total total
答案 0 :(得分:1)
查看您的代码,不仅仅是简单的解决方案:
echo "<b>Year {$reverse[0]}-{$reverse[count($reverse)-1]}</b>";
您想要输出$reverse
数组的第一个和最后一个元素。