简体   繁体   English

数据库架构 Treeview PHP MYSQL

[英]Database Schema Treeview PHP MYSQL

I am trying to create a treeview (ideally using bootstrap) from a MYSQL database table that is essentially a data dictionary structure.我正在尝试从本质上是数据字典结构的 MYSQL 数据库表创建 treeview(最好使用引导程序)。 Every example I have seen creates a parentid in order to create the json/array input for the treeview however my data structure has all of the hierarchy levels on each line.我看到的每个示例都创建了一个 parentid,以便为 treeview 创建 json/array 输入,但是我的数据结构在每一行都有所有层次结构级别。

The hierarchy would be:层次结构将是:

  • Database数据库

    • Schemas模式
      • Tables
  • Database数据库

    • Schemas模式
      • Tables

My database table has 3 columns... database, schema, and table.我的数据库表有 3 列...数据库、模式和表。 Each row has all three attributes, so the full hierarchy.每行都具有所有三个属性,因此是完整的层次结构。 What makes this a bit more tricky is the same schema and table can exist in multiple databases.使这更棘手的是相同的模式和表可以存在于多个数据库中。

Any ideas for how I should go about approaching this?关于我应该如何处理这个问题的任何想法?

Or perhaps as mentioned below how would I go from an array to a nested array of JavaScript objects that is the input of a treeview?或者也许如下所述,我将如何将 go 从数组转换为 JavaScript 对象的嵌套数组,该对象是 treeview 的输入?

Here is the php for creating the array:这是用于创建阵列的 php:

$stmt = $pdo->prepare('SELECT * FROM MyTable');
$stmt->execute();

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);

Database Structure and Data Image数据库结构和数据图像

You'll need to iterate through all rows and insert them into an appropriate map(associative array) in order to "inflate" the data structure.您需要遍历所有行并将它们插入到适当的映射(关联数组)中,以“膨胀”数据结构。

Something like this:像这样的东西:

//Fetch all rows from database into $result
$databases=[];
foreach($result as $row){
    $database=$row["database"];
    $schema=$row["schema"];
    $table=$row["object"];
    if(!array_key_exists($database, $databases))
        $databases[$database]=[];
    if(!array_key_exists($schema, $databases[$database]))
        $databases[$database][$schema]=[];
    array_push($databases[$database][$schema], $table);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM