我正在尝试使用jstree创建一个树,它将沿着表的列显示特定数据库的所有表。
现在这是我的脚本,我用它首先从数据库中获取表名,然后使用该表名来获取该表中可用的所有列。
<?php
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$post_data = array('id' => $dbname,'text' => $dbname, 'children' => array());
//echo "Connected successfully";
$sql = "SHOW tables";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$new = array();
array_push($new, array("id" => $row['Tables_in_test'], "text" => $row['Tables_in_test'], "children" => array()));
$sql1 = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$dbname."' AND TABLE_NAME = '".$row['Tables_in_test']."'";
$result1 = $conn->query($sql1);
while($row1 = $result1->fetch_assoc()) {
array_push($new[0]['children'], array("id" => $row1['COLUMN_NAME'], "text" => $row1['COLUMN_NAME']));
}
array_push($post_data['children'], $new);
}
} else {
echo "0 results";
}
print_r(json_encode($post_data));
$conn->close();
?>
现在,我以下列方式获取数据:
{
"id": "test",
"text": "test",
"children": [
[
{
"id": "accounts",
"text": "accounts",
"children": [
{
"id": "id",
"text": "id"
},
{
"id": "name",
"text": "name"
}
]
},
{
"id":"accounts_cases",
"text":"accounts_cases",
"children":[
{
"id":"id",
"text":"id"
},
{
"id":"account_id",
"text":"account_id"
}
]
}
],
],
}
现在,这种数据格式无法与jstree
一起使用。如您所见,第一个children
是一个数组,但由于脚本,它以某种方式将数组显示为数组。就像这样:
"text": "test",
"children": [
[
"id": "accounts",
这应该是这样的:
"text": "test",
"children": [
"id": "accounts",
我不知道如何解释这一点,但这应该是这样才能正常工作:
{
"id": "sugarcrm",
"text": "sugarcrm",
"children": [
{
"id": "accounts",
"text": "accounts",
"children": [
{
"id": "id",
"text": "id"
},
{
"id": "name",
"text": "name"
}
]
},
{
"id":"accounts_cases",
"text":"accounts_cases",
"children":[
{
"id":"id",
"text":"id"
},
{
"id":"account_id",
"text":"account_id"
}
]
}
],
}
我知道我的脚本有问题,但我无法纠正。所以,请帮助我。
答案 0 :(得分:0)
最后,我在奋斗了一天之后找到了解决方案。我没有使用另一个数组if ($result->num_rows > 0) {
// output data of each row
$i = 0;
while($row = $result->fetch_assoc()) {
array_push($post_data['children'], array("id" => $row['Tables_in_test'], "text" => $row['Tables_in_test'], "children" => array()));
$sql1 = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$dbname."' AND TABLE_NAME = '".$row['Tables_in_test']."'";
$result1 = $conn->query($sql1);
while($row1 = $result1->fetch_assoc()) {
array_push($post_data['children'][$i]['children'], array("id" => $row1['COLUMN_NAME'], "text" => $row1['COLUMN_NAME']));
}
$i++;
}
} else {
echo "0 results";
}
,而是发现在同一个主数组中我可以将新数组推入其中。在这里找到以下代码:
$i
在上面的代码中,使用array_push
计数器来检查获取最新的推送数组,如你所知,sudo -u gitlab-runner docker login xxx
插入数组并将键设置为0,1,...等整数所以一个。
希望它可以帮助有类似问题的人。