php - 从 PHP 类访问 HTML div id
问题描述
我正在尝试学习 PHP/Ajax,但我遇到了困难。我试图让我的 AJAX 请求的结果呈现在我的 PHP 类之外声明的 div 元素中。
我有一个 PHP 文件,其中包含一个名为 的类dashboard
,一旦它从 ajax 查询接收到 post 变量,它就会处理查询数据库,如下所示
public function loadTeamMembersChart($teamID, $lecturer_id, $teamColour){
$db = $this -> db;
if (!empty ($teamID) && !empty ($lecturer_id)){
$result = $db -> loadTeamMembersChart($teamID, $lecturer_id);
if ($result) {
$response["teamMember"] = $result;
$teamMembers = $_SESSION["teamMember"] = $result;
//Pass the results to the below function
loadTeamMembers($teamMembers, $teamColour);
} else {
$response["result"] = "failure";
$response["message"] = "Unable to Find Team Member Data";
return json_encode($response);
}
} else {
return $this -> getMsgParamNotEmpty();
}
}
然后在同一个 php 文件中,我有一个将结果传递给名为 loadTeamMembers 的全局函数,如下所示。
function loadTeamMembers($teamMembers, $teamColour){
//start to break up the teamMember object and store in variables
$teamMemberName = $_SESSION['teamMember']['all_team_member_names'];
$teamMemberPoints = $_SESSION['teamMember']['all_team_member_points'];
$teamMemberStudentNumber = $_SESSION['teamMember']['all_team_member_studentNumbers'];
$teamMemberLastActive = $_SESSION['teamMember']['all_date_last_present'];
$teamMemberTeamID = $_SESSION['teamMember']['all_team_ids'];
// The `$teamMemberData` array holds the chart attributes and data for the team object
$teamMemberData = array(
"chart" => array(
"caption" => "Student Team-Member Progress Chart",
"xAxisname"=> "Team-Member Name",
"yAxisName"=> "Points",
//Configure no.of visible plots
"numVisiblePlot"=> "5",
"theme"=> "zune",
"exportenabled"=> "1",
"exportMode"=> "server"
),
"categories" => array(
"category" => array()),
"dataset" => array(
"data" => array())
);
$teamMemberCount = 0;
// Push the data into the array
if (is_array($teamMemberName) || is_object($teamMemberName))
{
foreach($teamMemberName as $key => $value){
array_push($teamMemberData["categories"]["category"], array(
"label" => $teamMemberName[$teamMemberCount]." ".$teamMemberStudentNumber[$teamMemberCount]." Profile was last active on ".$teamMemberLastActive[$teamMemberCount]));
array_push($teamMemberData["dataset"]["data"], array(
"value" => $teamMemberPoints[$teamMemberCount],
"color"=> $teamColour));
$teamMemberCount++;
}
}
//encode the built team-member array so that it is returned to the ajax success request
echo $jsonEncodedTeamMemberData = json_encode($teamMemberData);
}
然后在同一个文件中,但在两个 PHP 类之外,我的 HTML 中有以下脚本:
<script>
function getTeamMembers(teamID,lecturer_id, teamColourCode){
//Variables needed to query the external DB to return required data to populate the charts
var teamInfo = {
"teamID" : teamID,
"lecturer_id" : lecturer_id,
"teamColourCode" : teamColourCode
};
/*
The below is used for the 'was a student present for the most recent quiz' pie chart. A boolean is set so that the post request knows
that we only need to call the loadIfTeamMemberWasPresent(); function, as the data was already obtained in the first ajax request. However, we don't want
to use that data on the first ajax call.
*/
var teamDetails = {
"teamClicked" : true
};
//Below is the first ajax call
$.ajax({
data: teamInfo,
url: 'dashboard.php',
type: 'POST',
success : function(data) {
console.log(data)
chartData = data;
apiChart = new FusionCharts({
type: 'scrollColumn2d',
renderAt: 'team-member-chart-container',
width: '550',
height: '350',
dataFormat: 'json',
dataSource: data
});
apiChart.render();
},
//Once the first ajax call has completed, we can then call the second ajax request to populate the pie-chart graph
complete:function(){
$.ajax({
data: teamDetails,
url: 'dashboard.php',
type: 'POST',
success : function(data) {
console.log(data)
chartData = data;
apiChart = new FusionCharts({
type: 'pie2D',
renderAt: 'teamMember-present-container',
width: '550',
height: '350',
dataFormat: 'json',
dataSource: data
});
apiChart.render();
},
});
}
});
}
</script>
但是,在我写的地方,我$columnChartTeamMember =...
无法在名为 的 div id 中访问和呈现我的图表team-member-chart-container
。
如果可能的话,我试图在类本身中不包含 HTML,因为其他类中的其他图表也需要在其他 div id 中呈现,例如individual-student-container
.
我曾尝试阅读此处找到的 PHP Simple HTML DOM Parser Manual ,但作为初学者,这让我有些困惑。我也不确定这是否是我所追求的。
我的方法基于 fusioncharts 给出的官方文档,可以在这里找到。
如果有人可以就我如何在提到的 div-id 中呈现我的图表提供一些指导,我将不胜感激。
编辑
最后,我设法通过将从我的 PHP 脚本中检索到的数据回显到 AJAX 请求,然后从success
调用 ajax 请求构建我的图表来解决我的问题。我已经更新了我的代码以显示这是如何实现的。我省略了对第二个 ajax 请求的处理,因为在第一次调用时已经检索到数据。但是,我还不想检索它,所以我在第一个请求完成后提出了第二个请求。现在一切正常。
解决方案
这方面的东西应该适合你。
PHP 文件: class.teamMember.php
<?php
require_once "fusioncharts.php";
class TeamMember {
public __construct($data) {
self::loadTeamMembers($data)
}
public function loadTeamMembers($data) {
$columnChartTeamMember = new \FusionCharts("scrollColumn2d", "teamMemberChart", 500, 300, "team-member-chart-container", "json", json_encode($data));
$columnChartTeamMember->render();
}
}
new TeamMember($data);
仍然需要$data
从某个地方提供它。看起来你是通过 : 来做的jQuery.post
,但是因为你没有运行function
: getTeamMembers
,teamInfo
所以总是空的,所以我把那部分排除在外。
HTML 文件: chart.html
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div id='unique-id'>Charts will load here.</div>
<script>
jQuery
.get('dashboard.php')
.then(function (data) {
jQuery('#unique-id').html(data);
});
</script>
</body>
</html>
假设以下是$data
(从 FusionCharts 复制/过去):
$data = "{
"chart":{
"caption":"Harry\'s SuperMart",
"subCaption":"Top 5 stores in last month by revenue",
"numberPrefix":"$",
"theme":"ocean"
},
"data":[
{
"label":"Bakersfield Central",
"value":"880000"
},
{
"label":"Garden Groove harbour",
"value":"730000"
},
{
"label":"Los Angeles Topanga",
"value":"590000"
},
{
"label":"Compton-Rancho Dom",
"value":"520000"
},
{
"label":"Daly City Serramonte",
"value":"330000"
}
]
}";
json_encode
你在课堂上不需要它。(换句话说,可以删除)但是现在:
$data = json_decode($data, true);
new TeamMember($data);
它应该显示在#unique-id
<div>
.
推荐阅读
- javascript - AXIOS VueJs 在 v-for 中传递响应参数
- android - Flutter:构建 APK 时如何传递 Gradle 参数?
- python - NumPy 数组中相同行的索引
- matlab - (应用程序设计器)使用 matlab.ui.control.EditField/set.Value 时出错(第 96 行)“值”必须是字符向量
- c# - 什么是迭代嵌套对象集合的更清洁或更有效的方法
- android - 如何添加按钮并将其覆盖在我的 Google 地图片段上
- c# - 使用接口类型定义的属性从动态映射到类型
- anaconda - 在 Windows 上安装 RStudio 的 anaconda 提示管理员
- c++ - 使用构造函数更改向量类型?
- bazel - 在 Windows 上使用 Bazel 0.21 构建 angular-bazel-example 的权限被拒绝