0

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 ループの方が優れていますか?

ありがとう

4

1 に答える 1

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];
    });
}
于 2013-11-02T14:25:55.173 に答える