最近,我尝试在我一直在从事的项目中插入可视化分析,并且我希望能够从 HTML form 中的 UI 创建图形。我的HTML页面的代码如下:
Charts
处理表单的文件是getData.php
18000)) {
// last request was more than 30 minutes ago
session_unset(); // unset $_SESSION variable for the run-time
session_destroy(); // destroy session data in storage
$scripttimedout = file_get_contents('timeout.js');
echo "";
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp?>
charts
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// get data from the form
$createGraph = $_POST['makeGraph'];
$attributes = $_POST['attributes'];
$charts = $_POST['charts'];
if (isset($_POST['makeGraph'])) {
try {
$array = array();
if ($charts == 'Pie_chart'){
if($attributes == 'Patient_name'){
$sql = "SELECT Patient_name, count(*) as number
FROM patients JOIN MSR ON patients.Patient_id = MSR.NDSnum
WHERE Doctor_ID = '$usersid' GROUP BY Patient_name";
$result = $pdo->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
while($row = $result->fetch()){
$array[] = $row;
}
$jsonArray = json_encode($array,JSON_PRETTY_PRINT);
echo $jsonArray;
}
// hoping to make something similar in the if statements with the rest of the attributes (email-sex-Race etc...)
} catch (PDOException $e) {
echo"";
echo $statement . "
" . $e->getMessage();
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
echo "";
}
}
?>
我的目标是让用户在第一页的html形式中输入他想要的图表类型和属性,然后让第二页处理请求并打印图表。
我知道我发布了很多代码,但我已经与这个问题斗争了 3 天,欢迎任何帮助!
编辑
我添加了定义 $_SESSION['LAST_ACTIVITY'] = time(); 和 $usersid 的部分。
问题是我似乎无法找到一种方法从表单中获取数据并使用它们来创建图表。当我创建
query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
while($row = $result->fetch()){
// $array[] = $row;
echo "['".$row['Patient_name']."',".$row['number']."],";
}?>
drawChart() 函数内的标记我的整个 getData.php 页面完全变成白色。我知道这不是解释我的问题的好方法,但我尽力了。请向我询问您需要的任何解释。
编辑2
在@Professor Abronsius 回答更改后,我可以打印饼图,不幸的是它看起来像这样:
当我尝试查看网络流量以找出 json 文件的样子时,该文件看起来不错:
0 Object { name: "Name1", number: "1" }
1 Object { name: "Name2", number: "1" }
2 Object { name: "Name3", number: "1" }
3 Object { name: "Name4", number: "1" }
4 Object { name: "Name5", number: "1" }
5 Object { name: "name6", number: "1" }
6 Object { name: "Name7", number: "1" }
7 Object { name: "Name8", number: "1" }
这意味着应该打印一个具有相等部分的饼图..有什么想法吗?
编辑3
生成图表时,以及在以下行添加 console.log() 后,控制台中没有错误:
Object.keys( json ).forEach(key=>{
console.log(dataTbl.addRow( [ json[ key ].name, json[ key ].number ] ));
})
控制台中的输出如下(我不明白它是对还是错):
Object { cq: null, bf: (2) […], Wf: (1) […], Br: null, cache: [], version: "0.6" }
Br: null
Wf: Array(8) [ {…}, {…}, {…}, … ]
0: Object { c: (2) […] }
c: Array [ {…}, {…} ]
0: Object { v: "Athanasia Moutlia" }
1: Object { v: "1" }
length: 2
: Array []
: Object { … }
1: Object { c: (2) […] }
2: Object { c: (2) […] }
3: Object { c: (2) […] }
4: Object { c: (2) […] }
5: Object { c: (2) […] }
6: Object { c: (2) […] }
7: Object { c: (2) […] }
length: 8
: Array []
bf: Array [ {…}, {…} ]
cache: Array(8) [ (2) […], (2) […], (2) […], … ]
cq: null
version: "0.6"
: Object { constructor: gvjs_M(a, b), ca: ca(), "$": $()
, … }
visual_analytics_google.php:203:13
Object { cq: null, bf: (2) […], Wf: (2) […], Br: null, cache: [], version: "0.6" }
visual_analytics_google.php:203:13
Object { cq: null, bf: (2) […], Wf: (3) […], Br: null, cache: [], version: "0.6" }
visual_analytics_google.php:203:13
Object { cq: null, bf: (2) […], Wf: (4) […], Br: null, cache: [], version: "0.6" }
visual_analytics_google.php:203:13
Object { cq: null, bf: (2) […], Wf: (5) […], Br: null, cache: [], version: "0.6" }
visual_analytics_google.php:203:13
Object { cq: null, bf: (2) […], Wf: (6) […], Br: null, cache: [], version: "0.6" }
visual_analytics_google.php:203:13
Object { cq: null, bf: (2) […], Wf: (7) […], Br: null, cache: [], version: "0.6" }
visual_analytics_google.php:203:13
Object { cq: null, bf: (2) […], Wf: (8) […], Br: null, cache: [], version: "0.6" }
visual_analytics_google.php:203:13
在层内:wf->0->c && 缓存,我可以看到我期望填充 dataTbl 的数据,但我不知道其余的是什么...
提前谢谢您!
getData.php 文件的 PasteBin 链接
html-form.php 文件的 PasteBin 链接
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
以下内容均未经过测试,但可能会帮助您找到解决方案。我建议不要以传统方式提交表单,而是将
getData.php和 HTML/javascript 完全分开。getData.php应该只运行 SQL 查询并将数据发送回 ajax 回调,然后调用drawChart来实际构建图表。getData.php
18000 ) ) ) { session_unset(); session_destroy(); exit( header('Location: ?timeout=true') ); } $_SESSION['LAST_ACTIVITY'] = time(); if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['attributes'], $_POST['charts'] )){ $pdo = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password ); $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $args=array(); # default, empty array if there are no placeholders $attributes = $_POST['attributes']; $charts = $_POST['charts']; /* create suitable SQL statement and $args array for each $attribute that you wish to plot on a chart. Use a Prepared statement to mitigate SQL injection attacks. */ switch( $attributes ){ case 'Patient_name': $sql='SELECT Patient_name, count(*) as number FROM patients JOIN MSR ON patients.Patient_id = MSR.NDSnum WHERE Doctor_ID = :userid GROUP BY Patient_name'; $args=array( ':userid' => $userid ); break; /* test cases... */ case 'maps': $sql='select `county` as `name`, count(*) as `number` from `vwmaps` group by `county`'; break; case 'fish': $sql='select `family` as `name`, count(*) as `number` from `vwfishspecies` group by `species_order_id`'; break; /* etc etc */ } if( isset( $sql, $args ) ){ $stmt=$pdo->prepare( $sql ); if( $stmt ) { $stmt->execute( $args ); http_response_code( 200 ); exit( json_encode( $stmt->fetchAll( PDO::FETCH_OBJ ) ) ); } exit( http_response_code( 400 ) ); } # no sql to run... http_response_code( 400 ); } # only allow POST requests http_response_code( 404 ); ?>以及
slim版本的 HTML 页面,没有外部库或 CSS,仅用于演示:进行了微小的更改并构建了测试页面,运行应用程序产生了: