我有一个名为end_users的表。我正在显示当年每月的注册用户数。我从数据库中获取数据,并以条形图形式显示当年1月至12月的数据。例如,当前年份为2019,因此条形图将显示2019年每个月有多少用户注册。
我想做的是根据给定用户的年份显示过去的年份数据,而不是仅仅显示当前年份。这个想法是用户将从下拉列表中选择年份,并将该年份传递到sql查询中。
我的模特
public function registered_user_graph(){
//This query fetch number of registered users from jan to dec of current year
$query = $this->db->query('SELECT (DATE_FORMAT(created_at,"%M")) AS "Month", COUNT(*) AS Number_of_registered_users FROM end_users WHERE year(created_at)= year(CURRENT_DATE) GROUP BY (DATE_FORMAT(created_at,"%M")) ORDER BY "Month" ASC');
if($query->num_rows() > 0){
return $query->result_array();
}else
return false;
}
我的观点
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<?php
$userPoints = array();
foreach ($userschart as $key => $value) {
$userPoints[$key]['label'] = $value['Month'];
$userPoints[$key]['y'] = $value['Number_of_registered_users'];
}
?>
<script type="text/javascript">
//for chart and graph
window.onload = function () {
//Chart of monthly registered user data
var charts = new CanvasJS.Chart("user_container", {
animationEnabled: true,
exportEnabled: true,
theme: "light1", // "light1", "light2", "dark1", "dark2"
title:{
text: "Monthly registered Users"
},
data: [{
type: "column", //change type to bar, line, area, pie, etc
//indexLabel: "{y}", //Shows y value on all Data Points
indexLabelFontColor: "#5A5757",
indexLabelPlacement: "outside",
dataPoints: <?php echo json_encode($userPoints, JSON_NUMERIC_CHECK); ?>
}]
});
chart.render();
charts.render();
}
</script>
我认为不需要控制器,以防万一,这是我的控制器
//Looping monthly registered users data got from model
$userschart = $this->dashboard_model->registered_user_graph();
$monthlyusers = array();
foreach($userschart as $row){
$monthlyusers[] = $row;
}
$this->data['userschart'] = $monthlyusers;
$this->template->admin_render('admin/dashboard/index',$this->data);
答案 0 :(得分:0)
尝试一下,将去年添加到$ userInput
function registered_user_graph($userInput = "") {
if(empty($userInput)) {
$userInput = date("Y");
}
//This query fetch number of registered users from jan to dec of current year
$query = $this->db->query('SELECT (DATE_FORMAT(created_at,"%M")) AS "Month", COUNT(*) AS Number_of_registered_users FROM end_users WHERE year(created_at)= ' . $userInput . ' GROUP BY (DATE_FORMAT(created_at,"%M")) ORDER BY "Month" ASC');
if($query->num_rows() > 0){
return $query->result_array();
}else
return false;
}