0

データベースから json 配列にデータをクエリして、最終的には JavaScript で力有向グラフを生成したいと考えています。最終結果で Json 配列がどのように見えるかを次に示します。ただし、ノードには複数の隣接関係がある場合とない場合があります。隣接関係セクションがノードごとに異なり、ノードの隣接関係の数に応じて調整できる json 配列をクエリするにはどうすればよいですか?

Var JSON =[ 
 {
  "adjacencies": [
    {
      "nodeTo": "graphnode9", 
      "nodeFrom": "graphnode5", 
      "data": {}
    }
  ], 
  "data": {
    "$color": "#416D9C", 
    "$type": "star"
  }, 
  "id": "graphnode5", 
  "name": "graphnode5"
},
];

これまでの私の試みは次のとおりですが、これは 1 つの隣接関係のみを許可する json のみを生成します。ノードが持つ隣接関係の数を調整し、それ自体を 1 つのセット配列形式または配列構造に制限しない Json クエリをセットアップするにはどうすればよいですか? ?

これが私のデータベース構造です

nodes                 Relationships                      
-----                 -------------
id int(11),           id int(11),
name varchar(35),     to int(11), //this is the destination node from the id relation 
color varchar(7),     data varchar(0) null
type varchar (12),    Foreign key (id) references nodes(id)
Primary key (id)       

engine = innodb    

そして、ここに私の試みがあります

function getjson()
{
 $db = adodbConnect();
 $query = "SELECT nodes.*, relationships.* FROM nodes LEFT JOIN relationships ON nodes.id    = relationships.id";
 $result = $db -> Execute($query);
 $count=$result->RowCount();
 $row=$result->FetchRow();

 $array= array[];

 while ($row)
  {
  $id= $row['id'];
  $name = $row['name'];
  $color1 = $row['color'];
  $type1 = $row['type'];
  $to=$row['to'];
  $array = [$id,$name,$color1,$type1,$to];
 }
 json_encode($array)
 }    
4

0 に答える 0