希望有人能找到我的问题的解决方案。我正在尝试使用谷歌地图和PHP创建一个仪表板。在我尝试将其放入MVC模式之前,我能够显示图形,现在我得到的只是错误。(代码分为各自的文件夹Model、View和Controller,根目录中有一个索引文件)。这是我迄今为止的代码
索引
<?php
include_once ("Design/includes.php");
$controller = new Controller();
$controller->invoke();
?>
控制器
<?php
class Controller {
public $model;
public function __construct()
{
$this->model = new tableModel();
}
public function invoke()
{
$this->model -> tableData();
include 'view/homeScreen.php';
}
}
?>
型号(这是针对表格的,另一个型号在同一文件夹中,但完全相同)
<?php
class tableModel {
public function tableData(){
global $con;
$result = mysqli_query ( $con, "SELECT fk_EXECUTION_LAB_MACHINE as LAB_MACHINE, COUNT(fk_EXECUTION_LAB_MACHINE) as TOTAL
FROM EXECUTION_QUEUE
WHERE fk_EXECUTION_LAB_MACHINE IS NOT NULL and WEEK (TEST_START_TIME) = WEEK( current_date ) -1 AND YEAR( TEST_START_TIME) = YEAR( current_date )
GROUP BY fk_EXECUTION_LAB_MACHINE order by count(fk_EXECUTION_LAB_MACHINE) desc;");
$rows = array();
$table = array();
$table['cols'] = array(
// Labels for chart/column titles
array('label' => 'LAB_MACHINE', 'type' => 'string'),
array('label' => 'TOTAL', 'type' => 'number')
);
//Getting results array and populating table
$rows = array();
while($r = mysqli_fetch_array($result)) {
$temp = array();
$temp[] = array('v' => (string) $r['LAB_MACHINE']);
$temp[] = array('v' => (int) $r['TOTAL']);
$rows[] = array('c' => $temp);
}
//Get table in JSON format
$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
return $jsonTable;
mysqli_close ( $con );
}
}
?>
这是的视图
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"
src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
var Dash = {
widget : {},
render : {
pie : function() {
var view = new google.visualization.DataView(Dash.data.pieEg);
var options = {
animation : {duration : 300},
chartArea : {width : '100%', height : '70%'},
is3D: 'true',
title : 'Pie Chart',
tooltip : {showColorCode : true}
};
Dash.widget.pie.draw(view, options);
},
table : function() {
Dash.widget.table = new google.visualization.ChartWrapper({
chartType: 'Table',
dataTable: Dash.data.tableEg,
options: {
allowHtml : true
},
containerId: 'table_div'
});
Dash.widget.table.draw();
}
},
util : {
bootstrap : function(){
Dash.util.prepareData();
Dash.render.table();
Dash.util.createWidgets();
Dash.util.refreshCharts();
},
createWidgets : function(){
Dash.widget.pie = new google.visualization.PieChart(document.getElementById('pie_div'));
},
refreshCharts : function(tableEg){
if (!tableEg || tableEg.length === 0) {
tableEg = [1,2];
}
Dash.render.pie(tableEg.slice(0));
},
prepareData : function(){
Dash.data = {};
var tableData = $.ajax({
url: "tableModel.php",
dataType:"json",
async: false
}).responseText;
Dash.data.tableEg = new google.visualization.DataTable(tableData);
var chartData = $.ajax({
url: "pieModel.php",
dataType:"json",
async: false
}).responseText;
Dash.data.pieEg = new google.visualization.DataTable(chartData);
}
}
};
google.load('visualization', '1', {
'packages':['corechart'],
'callback': Dash.util.bootstrap
});
</script>
</head>
<body>
<div id="row1">
<div id="pie_div"
style="width: 410px; height: 410px; display: inline-block"></div>
</div>
<div id="row2" style="width: 900px">
<div id="table_div" style="width: 700px; display: inline-block"></div>
<div id="gauge" style="display: inline-block; float: right"></div>
</div>
</body>
</html>
正如我之前所说,当我把视图和表数据放在一个文件夹中时,这些图工作得很好,但现在我把它放进了MVC风格,它不再工作了。这是我得到的错误
{"cols":[{"label":"LAB_MACHINE","type":"string"},{"label":"TOTAL","type":"number"}],"rows":[{"c":[{"v":"LIT-QAVIST-20"},{"v":1}]},{"c":[{"v":"LIT-QAVIST-25"},{"v":1}]}]}
Warning: mysqli_query(): Couldn't fetch mysqli in C:'Users'n0237569'workspace'Dashboard MVC'Design'model'tableModel.php on line 11
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:'Users'n0237569'workspace'Dashboard MVC'Design'model'tableModel.php on line 28
{"cols":[{"label":"LAB_MACHINE","type":"string"},{"label":"TOTAL","type":"number"}],"rows":[]}
Warning: include(view/homeScreen.php): failed to open stream: No such file or directory in C:'Users'n0237569'workspace'Dashboard MVC'Design'controller'Controller.php on line 17
Warning: include(): Failed opening 'view/homeScreen.php' for inclusion (include_path='.;C:'php'pear') in C:'Users'n0237569'workspace'Dashboard MVC'Design'controller'Controller.php on line 17
正如您从第一个错误中看到的那样,它以json形式返回数据,但我不确定接下来会发生什么。
这是包含文件夹的内容,因为最后两个错误引用了
包括
<?php
require_once "Design/config.php";
require_once "Design/connection.php";
require_once "Design/model/pieModel.php";
require_once "Design/model/tableModel.php";
require_once "Design/controller/Controller.php";
?>
如有任何帮助,将不胜感激
感谢
Warning: include(view/homeScreen.php): failed to open stream: No such file or directory in C:'Users'n0237569'workspace'Dashboard MVC'Design'controller'Controller.php on line 17
Warning: include(): Failed opening 'view/homeScreen.php' for inclusion (include_path='.;C:'php'pear') in C:'Users'n0237569'workspace'Dashboard MVC'Design'controller'Controller.php on line 17
这些错误是由您的控制器中的这条线引起的
include 'view/homeScreen.php';
因为在你的控制器中,你在Controller
文件夹中,所以你需要查找一个级别,即
include '../view/homeScreen.php';
在您的表格中型号
return $jsonTable;
mysqli_close ( $con );
tableModel
底部的mysqli_close( $con );
部分是无用的,因为你已经在它上面的return
了。当请求完成时,PHP会自动关闭连接,但如果不再需要,可能值得自己关闭它。
您应该在MySQL函数周围封装一些逻辑来调试查询。
你可以试试
if( !$result ) { die(mysqli_error($con));
您应该考虑使用PDO,并查看一些现有的MVC框架,以更好地了解MVC模式,以及它们如何处理路由和自动加载类等。