以下是php和js:
createRoomTable()
function createRoomTable(){
$.post('../include/returnRoomTable.php',{
//nothing to transmit
}).then((returnedTableMarkup) => {
returnedTableMarkup = JSON.parse(returnedTableMarkup)
console.log("data from returnRoomTable.php is ", returnedTableMarkup)
//$('#roomTableOutput').html(returnedTableMarkup)
})
}
<?php
session_start();
try{
$connection = new PDO('mysql:host=localhost;dbname=XXXX',
'XXXX','XXXXXX');
}catch(PDOException $e){
echo $e->getMessage();
}
$allRooms = $connection->query("
SELECT name
FROM raum
")->fetchAll(PDO::FETCH_NUM);
$indexLimit = count($allRooms);
$allSeats = [];
for($index = 0; $index < $indexLimit; $index++){
$allSeats = array_push($connection->query("
SELECT nummer
FROM arbeitsplatz
WHERE raum = '".$allRooms[$index]."'
")->fetchAll(PDO::FETCH_NUM));
}
echo json_encode ($allSeats);
?>
因此,目前,consolelog表示该数组为“ null”。 我需要的是一个灵活的二维数组(“ $ allSeats”),该数组将从MYSQL查询中获取每次迭代并将其放入此数组中。 问题是我对php中的数组不是很熟悉,而且我不知道如何实现此目的。
答案 0 :(得分:0)
好的,所以我自己找到了解决问题的方法:
php现在看起来像这样:
<?php
session_start();
try{
$connection = new PDO('mysql:host=localhost;dbname=arbeitsplatzverwaltung',
'verwalter','N7pY1OTl2gaBbc51');
}catch(PDOException $e){
echo $e->getMessage();
}
$allRooms = $connection->query("
SELECT name
FROM raum
")->fetchAll(PDO::FETCH_NUM);
$indexLimit = count($allRooms);
$allSeats = [];
for($index = 0; $index < $indexLimit; $index++){
$currentQuery = $connection->query("
SELECT nummer
FROM arbeitsplatz
WHERE raum = '".$allRooms[$index][0]."'
")->fetchAll(PDO::FETCH_NUM);
array_push($allSeats, $currentQuery);
}
/*
$allSeats[] = $connection->query("
SELECT nummer
FROM arbeitsplatz WHERE raum = '".$allRooms[$index]."'
")->fetchAll(PDO::FETCH_NUM);*/
echo json_encode ($allSeats);
?>
当我在JS中输出此代码时,我得到一个包含值的数组数组。