我有2個陣列
$dates = array('2015-08-10','2015-08-11','2015-08-12','2015-08-13');
$datas = array('a'=>'2015-08-11_lvl4b,2015-08-13_lvl4b','b'=>'2015-08-10_lvl1b,2015-08-11_lvl3b,2015-08-12_lvl4b');
$dates 是一個日期區間
$datas 是多筆資料組成的陣列
我是想把上面弄成table顯示
大致上像圖這樣子
請問怎麼寫比較好,感謝大家
andyto202提到:
請問怎麼寫比較好
就用php寫呀,還能怎麼寫?
這樣的發問,很像是直接來伸手要程式的,沒顯示你有思考過該怎麼解,不會的點在哪?下次又遇到類似問題,又是一句『怎麼寫比較好?』你會永遠無法獨立開發喔。
下面就是個解。有哪裡很難,無法理解的嗎?
<pre class="c" name="code"><?php
$dates = array('2015-08-10','2015-08-11','2015-08-12','2015-08-13');
$datas = array('a'=>'2015-08-11_lvl4b,2015-08-13_lvl4b','b'=>'2015-08-10_lvl1b,2015-08-11_lvl3b,2015-08-12_lvl4b');
echo '<table><tr><td></td>';
foreach($dates as $d) echo '<td>',$d,'</td>';
echo "</tr>\n";
$L = count($dates);
foreach($datas as $k => $d)
{
echo '<tr><td>',$k,'</td>';
for ($i=0; $i<$L; $i++)
echo '<td>',(($p = strpos($d, $dates[$i])) === false ? '無資料' : substr($d, $p+11, 5)),'</td>';
echo "</tr>\n";
}
echo '</table>';
?>
<pre class="c" name="code">
$dates = array('2015-08-06','2015-08-07','2015-08-08','2015-08-09','2015-08-10','2015-08-11','2015-08-12','2015-08-13');
$datas = array('a'=>'2015-08-11_lvl4b,2015-08-13_lvl4b','b'=>'2015-08-10_lvl1b,2015-08-11_lvl3b,2015-08-12_lvl4b');
foreach($datas as $key => $data)
{
$a = explode(",",$data);
foreach($dates as $date)
{
if(!preg_grep('/'.$date.'/',$a))
{
array_push($a,$date."_".'沒資料');
}
}
sort($a);
$ok = implode(",",$a);
$temp_datas[] = array($key=>$ok);
}
foreach($temp_datas as $line)
{
foreach($line as $key => $line2)
{
$datas2[$key] = $line2;
}
}
<pre class="c" name="code">
echo "<table border='1'>";
echo "<tr>";
echo "<td></td>";
foreach($dates as $date)
{
echo "<td>";
echo $date;
echo "</td>";
}
echo "<tr>";
foreach($datas2 as $key => $line2)
{
echo "<tr>";
$values = explode(",",$line2);
echo "<td>".$key."</td>";
foreach($values as $value)
{
list($date_data,$color_class) = explode("_",$value);
echo "<td>";
echo $color_class;
echo "</td>";
}
echo "<tr>";
}
echo "</table>";
我寫出來了
沒辦法像你這樣子簡潔有力
_<