溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP如何實現(xiàn)動態(tài)柱狀圖改進版

發(fā)布時間:2021-06-25 14:16:58 來源:億速云 閱讀:103 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)PHP如何實現(xiàn)動態(tài)柱狀圖改進版的內(nè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> 
  <h4 align="center">近一個月總的情況</h4> 
  <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 ></div> 
    </td> 
    <td> 
    <p><?php echo $d4[0]?></p> 
    <div ></div> 
    </td> 
    <td> 
    <p><?php echo $d2[0]?></p> 
    <div ></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> 
  <h4 align="center">近一個月每個人的情況</h4> 
  <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如何實現(xiàn)動態(tài)柱狀圖改進版

感謝各位的閱讀!關(guān)于“PHP如何實現(xiàn)動態(tài)柱狀圖改進版”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI