我的DBMS(sql server 2012)中有两个表:pointsBadgeTable和EmployeeTable。
我使用以下查询进行了下面的ajax调用,并且能够成功检索数组。
<?php
session_start();
$servername = "abc";
$connectioninfo = array(
'Database' => 'novemberLastWeek'
);
$conn = sqlsrv_connect($servername, $connectioninfo);
if (!$conn) {
echo 'connection failure';
die(print_r(sqlsrv_errors() , TRUE));
}
if(isset($_POST['id']) && is_numeric($_POST['id'])){
$q1 = "select * from pointsBadgeTable WHERE EmployeeID = '".$_POST['id']."' and WeekNumber='week51' order by pointsRewarded desc";
// and convert it to JSON like your script above so your javascript does the rest.
// _+_______________________________________________________________________________--
if(($stmt = sqlsrv_query($conn, $q1)) != false){
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$result[] = $row;
}
} while (sqlsrv_next_result($stmt));
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn); //Close the connnectiokn first// find the code and paste it in there
echo json_encode($result);
} else {
die(print_r(sqlsrv_errors(), true)); //try
}
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
?>
现在我在同一个数据库中的另一个表中还有一个查询:
select * from EmployeeTable;
ajax电话:
$('.leaderboard li').one('click', function () {
$.ajax({
url: "../popupData/dataWeekly.php",
type: "POST",
data: {id:$(this).find('.parent-div').data('id')},
success: function(data){
console.log(data);
data = JSON.parse(data);
$('#popup').fadeIn();
$('#managerid').text('Reporting Manager: ' + data[0].ReportingManager);
$('#popup-name').text('Name: ' + data[0].EmployeeName);
$('#weeklypoints').text('Recent Weekly Points :' + data[0].pointsRewarded +' ' +"(" +data[0].WeekNumber+")");
$('#weeklyrank').text('Recent Weekly Rank :' + data[0].rank +' ' +"(" +data[0].WeekNumber+")");
//$('#comment').text('Comment weekly: ' + data[0].Comment);
$('#gold').text(' ' + data[0].Gold);
$('#Silver').text(' ' + data[0].silver);
$('#Bronze').text(' ' + data[0].bronze);
// etc ..
},
error: function(){
alert('failed, possible script does not exist');
}
});
});
如何在上面的PHP中包含这两个查询,并将查询中的整个信息提取到一个数组中。
请帮助。
答案 0 :(得分:2)
您可以嵌套AJAX请求:
$.get("url1.php").done(function(data1){
$.get("url2.php").done(function(data2){
// Do whatever you want
});
});
答案 1 :(得分:1)
没有详细说明如何处理这里的数据是一个基本的例子,可以在一次调用中分组两个不同的数据:
PHP:
/*...*/
// Grouping the two vars under the same array.
echo json_encode(array(
"pointsBadges" => $pointsBadgesData,
'employees' => $employeesData
));
Javascript:
$('.leaderboard li').one('click', function () {
$.ajax({
url: "../popupData/dataWeekly.php",
dataType: 'json', // Will automaticaly parse JSON
type: "POST",
data: {
id: $(this).find('.parent-div').data('id')
},
success: function(data){
// For each pointsBadges, do [...]
$.each(data.pointsBadges, (key, value) => {
/*...*/
});
// For each employees, do [...]
$.each(data.employees, (key, value) => {
/*...*/
});
},
error: function(){
alert('failed, possible script does not exist');
}
});
});
</script>