PHP實現(xiàn)動態(tài)柱狀圖改進版
本文實例分析了PHP實現(xiàn)動態(tài)柱狀圖的改進版。分享給大家供大家參考。具體分析如下:
前面已經(jīng)寫過如果只做動態(tài)柱狀圖的情況,其實原理還是很簡單的。因為昨天下午有新的需求,今天上午又修改了一番,并將數(shù)據(jù)根據(jù)編號不同分割顯示在表中。
下面把代碼粘出來,方便以后自己查看,思路只是一時的火花,今天我想出來這么做,不一定下次還能想得到,也不用費勁的去想,所以寫成筆記是比較好的形式。
<!DOCTYPE html>
<?php
// 計算上一個月的今天
function last_month_today($time)
{
$last_month_time = mktime(date("G", $time), date("i", $time), date("s", $time), date("n", $time), 0, date("Y", $time));
$last_month_t = date("t", $last_month_time);
if ($last_month_t < date("j", $time)) {
return date("Y-m-t H:i:s", $last_month_time);
}
return date(date("Y-m", $last_month_time) . "-d", $time);
}
?>
<?php
include dirname(dirname(dirname(__FILE__))) . '/config.php';
$endDate = date('Y-m-d');
$date = strtotime($endDate);
$beginDate = last_month_today($date);
$sql = 'select count(*) from newpro where p_date>\'' . $beginDate . '\' and p_date<\'' . $endDate . '\'';
$d = db()->query($sql)->fetch(PDO::FETCH_NUM);
$sql2 = $sql . ' and is_pa_check_first=1 and is_pa_check_second=1 and is_pa_check_third=1';
$d2 = db()->query($sql2)->fetch(PDO::FETCH_NUM);
$sql3 = $sql . ' and is_pa_check_first=1';
$d3 = db()->query($sql3)->fetch(PDO::FETCH_NUM);
$sql4 = $sql . ' and is_pa_check_first=1 and is_pa_check_second=1';
$d4 = db()->query($sql4)->fetch(PDO::FETCH_NUM);
// 查詢每個人通過審核的情況:
$sqlab = 'select d_m,sum(sroce) as total_score,count(d_m) as total_number
from newpro
where is_pa_check_first=1
and is_pa_check_second=1
and is_pa_check_third =1
group by d_m';
$row = db()->query($sqlab)->fetchAll(PDO::FETCH_ASSOC);
?>
<html>
<head>
<meta charset="utf-8" />
<style>
div {
background-color: #669900;
width: 50px;
}
#div1 {
height: 200px;
}
#table td {
}
</style>
<script type="text/javascript" src="../../../js/jquery-1.7.2.min.js"></script>
</head>
<body>
<h3 align="center">近一個月總的情況</h3>
<table border="0" align="center" id="table1">
<caption>
<?php echo "時間:".$beginDate."至".$endDate?>
</caption>
<tr align="center" valign="bottom">
<td>
<p><?php echo $d[0]?></p>
<div id="div1"></div>
</td>
<td>
<p><?php echo $d3[0]?></p>
<div style="height:<?php $str=floor(($d3[0]/$d[0])*200); echo $str.'px'?>"></div>
</td>
<td>
<p><?php echo $d4[0]?></p>
<div style="height:<?php $str=floor(($d4[0]/$d[0])*200); echo $str.'px'?>"></div>
</td>
<td>
<p><?php echo $d2[0]?></p>
<div style="height:<?php $str=floor(($d2[0]/$d[0])*200); echo $str.'px'?>"></div>
</td>
</tr>
<tr align="center" valign="top">
<td><p>總計</p></td>
<td><p>一審?fù)ㄟ^</p></td>
<td><p>二審?fù)ㄟ^</p></td>
<td><p>審核通過</p></td>
</tr>
</table>
<h3 align="center">近一個月每個人的情況</h3>
<table border="0" width="100%">
<caption>每個人的完成情況如下表:</caption>
<!-- 因為總的列數(shù)比較長,如果顯示在一個表格中,數(shù)據(jù)會很擁擠,多的話根本就看不清楚。
所以需要將數(shù)據(jù)進行分割,根據(jù)長度進行動態(tài)的分割,顯示在多張表中。
-->
<?php
$arr = array_chunk($row,2,false);//2表示分割的單位長度,false表示索引從0開始
foreach($arr as $newRow){
$thStr = "<th style='background-color:#669900' width='110px' height='30px'>產(chǎn)品開發(fā)編號</th>";
$trStr_total_score = "<tr align='center' style='background-color:silver' height='25px'><td>總分</td>";
$trStr_total_number = "<tr align='center' style='background-color:silver' height='25px'><td>總數(shù)量</td>";
$trStr_average_score = "<tr align='center' style='background-color:silver' height='25px'><td>平均分</td>";
$resultStr = "";
foreach ($newRow as $key => $value) {
// echo $key."=>".$value."<br/>";
$x = 0;
foreach ($value as $key2 => $value2) {
// echo $key2 . "=>" . $value2 . "<br/>";
if ($key2 == 'd_m') {
$thStr .= "<th style='background-color:#669900'>" . $value2 . "</th>"; // 表頭
} elseif ($key2 == 'total_score') {
$value2 = sprintf("%.2f", $value2); //保留2位小數(shù)
$trStr_total_score .= "<td>" . $value2 . "</td>";
$x += $value2;
} elseif ($key2 == 'total_number') {
$trStr_total_number .= "<td>" . $value2 . "</td>";
$x /= $value2;
}
}
$x = sprintf("%.2f",$x);
$trStr_average_score .= "<td>" . $x . "</td>";
}
echo "<table border='0' width='100%'>";
echo $thStr;
echo $trStr_total_number . "</tr>";
echo $trStr_total_score . "</tr>";
echo $trStr_average_score . "</tr>";
echo "</table>";
echo "<p height='150px'></p>";
}
?>
</table>
</body>
</html>
數(shù)據(jù)庫方便就不弄了,其實,根據(jù)查詢的表名和字段名,是很容易建一個測試的數(shù)據(jù)表的。關(guān)鍵是思路,無論怎么變,思路是關(guān)鍵。
為了更加方便的了解代碼的效果,截個圖吧

希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
用mysql觸發(fā)器自動更新memcache的實現(xiàn)代碼
不錯的一篇文章,用于項目中可以帶來更多的便利,按照方法已經(jīng)調(diào)試成功,可以大大提高項目的速度。2009-10-10
詳解PHP結(jié)構(gòu)型設(shè)計模式之橋接模式Bridge Pattern
橋接,顧名思義,就是用來連接兩個部分,使得兩個部分可以互相通訊。橋接模式將系統(tǒng)的抽象部分與實現(xiàn)部分分離解耦,使他們可以獨立的變化。本文通過示例詳細介紹了橋接模式的原理與使用,需要的可以參考一下2023-04-04
解析PHPExcel使用的常用說明以及把PHPExcel整合進CI框架的介紹
本篇文章是對PHPExcel使用的常用說明以及把PHPExcel整合進CI框架的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06

