2017-06-28 46 views
1

我有這樣的代碼,將產生被分爲5年組each..from 2006年至2017年將自動航向上表的頂部

如何把標題上的頂部年的表表作爲示例下面..

<?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 

回答

1

看你的代碼,不只是簡單的解決方案:

echo "<b>Year {$reverse[0]}-{$reverse[count($reverse)-1]}</b>"; 

你要輸出的$reverse數組的第一個和最後一個元素。

+0

謝謝..但它沒有工作..我有一半的加載頁面 – blackrx

+0

糟糕,我有'$ count'而不是'count' - 是我的錯誤的問題? –

+0

是的,再次感謝..現在它的工作..我仍然是一個初學者..你真的幫了我很多。祝你今天愉快 – blackrx

相關問題