從數據庫中取出最近三十天的數據并生成柱狀圖的代碼,需要的朋友可以參考下。
在終端用cd 命令進入文件目錄
說明:此處例子我是拿項目中的一個例子講解的。
1、新建一個項目 :用終端輸入:zf create project Airline 格式:zf create action project project-name 備注:這些格式可以在終端輸入zf 查看
2、新建一個action :zf create action dirgramshow index 格式:zf create action action-name controller-name
3、新建一個 model :zf create db-table flightinformation
action 層代碼:indexController.php
代碼如下:
public function indexAction ()
{
// action body
$db = new Application_Model_DbTable_Flightinformation();
/*獲取最近30天內的數目
* select day(boo_time) as day,count(boo_autoid)as count,boo_time from bookinformation
where flag_pass=0 and date_sub(now(), interval 30 day)group by DATE_FORMAT(boo_time,’%m %d’)
*/
$sql = “select DATE_FORMAT(boo_time,’%m-%d’) as day,count(boo_autoid)as count from bookinformation ” .
“where flag_pass=0 and date_sub(now(), interval 30 day)”group by DATE_FORMAT(boo_time,’%m %d’)”;
$result = $db->getAllInfo($sql)->fetchAll();
$this->view->result=$result;
}
view 層代碼:dirgramshow.phtml
代碼如下:
model 層代碼:Flightinformation.php
代碼如下:
class Application_Model_DbTable_Flightinformation extends Zend_Db_Table_Abstract
{
protected $_name = ‘flightinformation’;
public function getAllInfo($sql){
$adapter = Zend_Registry::get(‘db’);
$flightinformation = $adapter->query($sql);
return $flightinformation;
}
}
最后的效果圖如下: