I am trying to create a tree using jstree which will show that all tables of a specific database along the columns of the tables.
Now this is my script, which I am using to fetch the table names first from the database and later using that table name, to fetch all the column are available in that table.
<?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();
?>
Now, I am getting data in the following manner:
{
"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"
}
]
}
],
],
}
Now, this format of data is not working with jstree. As you can see, the first children is an array but due the script it is somehow shows array into array. It is like this:
"text": "test",
"children": [
[
"id": "accounts",
this should be like this:
"text": "test",
"children": [
"id": "accounts",
I don't know how to explain this, but It should be like this to work properly:
{
"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"
}
]
}
],
}
I know that there is something wrong in my script, but I am unable to correct. So, kindly help me out here.