PHP實現動態柱狀圖改進版_php技巧

來源:互聯網
上載者:User

本文執行個體分析了PHP實現動態柱狀圖的改進版。分享給大家供大家參考。具體分析如下:

前面已經寫過如果只做動態柱狀圖的情況,其實原理還是很簡單的。因為昨天下午有新的需求,今天上午又修改了一番,並將資料根據編號不同分割顯示在表中。

下面把代碼粘出來,方便以後自己查看,思路只是一時的火花,今天我想出來這麼做,不一定下次還能想得到,也不用費勁的去想,所以寫成筆記是比較好的形式。

<!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>一審通過</p></td>     <td><p>二審通過</p></td>     <td><p>審核通過</p></td>   </tr>   </table>   <h3 align="center">近一個月每個人的情況</h3>   <table border="0" width="100%">   <caption>每個人的完成情況如下表:</caption>  <!-- 因為總的列數比較長,如果顯示在一個表格中,資料會很擁擠,多的話根本就看不清楚。      所以需要將資料進行分割,根據長度進行動態分割,顯示在多張表中。    -->     <?php     $arr = array_chunk($row,2,false);//2表示分割的單位長度,false表示索引從0開始     foreach($arr as $newRow){       $thStr = "<th style='background-color:#669900' width='110px' height='30px'>產品開發編號</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>總數量</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位小數         $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>

資料庫方便就不弄了,其實,根據查詢的表名和欄位名,是很容易建一個測試的資料表的。關鍵是思路,無論怎麼變,思路是關鍵。

為了更加方便的瞭解代碼的效果,截個圖吧

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.