用于获取Spacetree(jit)数据的MySQL查询

时间:2013-11-01 16:41:33

标签: php mysql json

我想从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(数据,函数(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或while while循环会更好?

由于

1 个答案:

答案 0 :(得分:1)

$.getJSON("storescripts/json.php", function(data) {
    var json = rows_to_tree(data);
    // Do stuff with json
});

function rows_to_tree(rows) {
    var id = 1;
    var result = { id: id++, name: "Student Records", data: {}, children: {} };
    var tree = result.children;
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        var student = row.name, course = row.course, cgpa = row.CGPA, grades = row.grades;
        // First create all the children as objects, to make searching easier
        tree[student] = tree[student] || { id: id++, name: student, data: {}, children: {} };
        tree[student].children[course] = tree[student].children[course] || { id: id++, name: course, data: {}, children: {} };
        tree[student].children[course].children[cgpa] = tree[student].children[course].children[cgpa] || { id: id++, name: cgpa, data: {}, children: {} };
        tree[student].children[course].children[cgpa].children[grades] = tree[student].children[course].children[cgpa].children[grades] || { id: id++, name: grades, data: {}, children: [] };
    }
    // Now convert the children objects to arrays
    result.children = obj_to_array(result.children);
    $.each(result.children, function(k, value) {
        value.children = obj_to_array(value.children);
        $.each(value.children, function(k, value2) {
            value2.children = obj_to_array(value2.children);
            $.each(value2.children, function(k, value3) {
                value3.children = obj_to_array(value3.children);
            });
        });
    });
    return result;
}

function obj_to_array(obj) {
    return $.map(obj, function(value) {
        return [value];
    });
}