我想从 MySQL 表中提取数据并转换为 JSON 格式。我所需要的只是像在 Spacetree 中一样从数据中创建一棵树。我正在使用的示例是:
http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.html
我在论坛里搜索过。我发现了一些东西,但不确定我是对还是错。请参阅以下内容:我创建了 json.php 如下
<?php
include_once("connect_to_mysql.php");
$query = "SELECT profile.name, profile.faculty, profile.course, subject.subject_id, subject.subject_name, grade.CGPA, grade.grades
FROM profile
LEFT JOIN subject on profile.stud_id = subject.stud_id
LEFT JOIN grade on subject.subject_id = grade.subject_id";
$result=mysql_query($query);
$_ResultSet = array();
while ($row = mysql_fetch_assoc($result)) {
$_ResultSet[] = $row;
}
echo json_encode($_ResultSet);
?>
然后在example1.js文件中进行如下修改
//init data
$.getJSON("storescripts/json.php", function(data) {
var add = 0;
var json = {
id: "node1",
name: "Students Names",
data: {},
$.each(data, function(i, j) {
// use: j.columnName
children: [{
id: add+1,
name: j.name,
data: {},
children: [{
id: add+1,
name: j.course,
data: {},
children: [{
id: add+1,
name: j.CGPA,
data: {},
children: [{
id: add+1,
name: j.grade,
data: {},
children: []
}]
}]
}]
}]
},
});
});
我不知道它是否正确。但是这里 $.each(data, function(i, j) { 它给出了错误。我想要的结果如下:
var json = {
id: 1,
name: "Students Records",
data: {},
children: [{
id: 2,
name: "Adam",
data: {},
children: [{
id: 3,
name: "Intro 2 Business",
data: {},
children: [{
id: 4,
name: "3.5",
data: {},
children: [{
id: 5,
name: "A+",
data: {},
children: []
}]
}]
}, {
id: 6,
name: "Marketing Essential",
data: {},
children: [{
id: 7,
name: "3.23",
data: {},
children: [{
id: 8,
name: "A",
data: {},
children: []
}]
}]
}]
}, {
id: 9,
name: "Peter",
data: {},
children: [{
id: 10,
name: "Image Processing",
data: {},
children: [{
id: 11,
name: "3.27",
data: {},
children: [{
id: 12,
name: "A",
data: {},
children: []
}, ]
}]
}, {
id: 13,
name: "Computer Vision",
data: {},
children: [{
id: 14,
name: "3.38",
data: {},
children: [{
id: 15,
name: "A",
data: {},
children: []
}, ]
}, ]
}, {
id: 16,
name: "Neural Networks",
data: {},
children: [{
id: 17,
name: "3.59",
data: {},
children: [{
id: 18,
name: "A",
data: {},
children: []
}, ]
}]
}]
}, {
id: 19,
name: "Luke",
data: {},
children: [{
id: 20,
name: "Opinion Mining",
data: {},
children: [{
id: 21,
name: "3.67",
data: {},
children: [{
id: 22,
name: "A",
data: {},
children: []
}, ]
}]
}, {
id: 23,
name: "Macine Learning",
data: {},
children: [{
id: 24,
name: "3.84",
data: {},
children: [{
id: 25,
name: "A",
data: {},
children: []
}]
}]
}, {
id: 26,
name: "Semantic Web",
data: {},
children: [{
id: 27,
name: "3.105",
data: {},
children: [{
id: 28,
name: "A",
data: {},
children: []
}]
}]
}, {
id: 29,
name: "NLP",
data: {},
children: [{
id: 30,
name: "3.110",
data: {},
children: [{
id: 31,
name: "A",
data: {},
children: []
}]
}]
}]
}, {
id: 32,
name: "Christopher",
data: {},
children: [{
id: 33,
name: "Poetry course",
data: {},
children: [{
id: 34,
name: "3.132",
data: {},
children: [{
id: 35,
name: "A",
data: {},
children: []
}]
}]
}, {
id: 36,
name: "Linguistics course",
data: {},
children: [{
id: 37,
name: "3.139",
data: {},
children: [{
id: 38,
name: "A",
data: {},
children: []
}]
}]
}]
}]
};
当我更改数据库表中的数据时,它应该是动态变化的。
任何帮助将不胜感激。
我确实喜欢关注,但没有显示任何内容。当创建一个单独的 php 页面时,它可以工作并以 JSON 形式显示结果。但在 .js 中它不起作用。
function init(){
//init data
//include .js file inside .js file
/*document.writeln("<script type='text/javascript' src='jquery/jquery.min.js'></script>");*/
/*var script = document.createElement('script');
script.src = 'jquery/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);*/
var imported = document.createElement('script');
imported.src = 'jquery/jquery.min.js';
document.head.appendChild(imported);
$.getJSON("storescripts/json.php", function(data) {
document.writeln("var json = {");
document.writeln('id: "node1",');
document.writeln('name: "Students Names",');
document.writeln("data: {},");
$.each(data, function(i, j) {
// use: j.columnName
for(var add = 0; add <= i; add++){
document.writeln("children: [{");
document.writeln("id:" + (++add) + ",");
document.writeln("name:" + j.name + ",");
document.writeln("data: {},");
document.writeln("children: [{");
document.writeln("id:" + (++add) + ",");
document.writeln("name:" + j.course + ",");
document.writeln("data: {},");
document.writeln("children: [{");
document.writeln("id:" + (++add) + ",");
document.writeln("name:" + j.CGPA + ",");
document.writeln("data: {},");
document.writeln("children: [{");
document.writeln("id:" + (++add) + ",");
document.writeln("name:" + j.grade + ",");
document.writeln("data: {},");
document.writeln("children: []");
document.writeln("}]");
document.writeln("}]");
document.writeln("}]");
document.writeln("}]");
}
});
document.writeln("};");
});
}
如果请可以帮助我。并告诉我我是否正确包含了 jquery.min.js 文件?for 循环在逻辑上是否正确?还是从技术上讲,while 或 do while 循环会更好?
谢谢