Getting issues creating valid JSON object to use for jstree
我正在尝试使用JStree创建一个树,它将显示特定数据库的所有表以及表的列。
现在,这是我的脚本,我使用它先从数据库中获取表名,然后使用该表名获取该表中的所有列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?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(); ?> |
现在,我以以下方式获取数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | { "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" } ] } ], ], } |
号
现在,这种数据格式不适用于
1 2 3 4 | "text":"test", "children": [ [ "id":"accounts", |
应该是这样的:
1 2 3 | "text":"test", "children": [ "id":"accounts", |
。
我不知道该如何解释,但应该这样才能正常工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | { "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" } ] } ], } |
我知道我的剧本有问题,但我无法改正。所以,请帮帮我。
最后,经过一天一夜的挣扎,我找到了解决办法。我发现,在同一个主数组中,我可以将新数组推到其中,而不是使用另一个数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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"; } |
在上面的代码中,使用
希望它能帮助有类似问题的人。