0

データベース内の私のデータ

ここに画像の説明を入力

現在、私のphpファイルは次のようなデータベースからデータを読み取っています

        $data = array();

        $sql = "SELECT * FROM tree";

        $q = mysql_query($sql);

        while ($r = mysql_fetch_array($q)) {
            // check if have a child node                           
            $qq = mysql_query("SELECT `id`, `text` FROM `tree` WHERE parent_id = '". $r['id'] ."'");

            if (mysql_num_rows($qq) > 0) {
                // if have a child
                $r['leaf'] = false;
                $r['cls'] = 'folder';
            } else {
                // if have no child
                $r['leaf'] = true;
                $r['cls'] = 'file';
            }
            $data[] = $r;

        }               


        echo json_encode($data);
    ?>
    <div id="tree_el"></div> 

私のJavaScriptは

    Ext.require([
        'Ext.tree.*',
        'Ext.data.*',
        'Ext.tip.*'
    ]);

    Ext.onReady(function() {
        Ext.QuickTips.init();

        var store = Ext.create('Ext.data.TreeStore', {
            proxy: {
                type: 'ajax',
                url: 'treegetdata.php'
            },
            root: {
                text: 'Eatables',
                id: 'root_node',
                expanded: true
            },
            folderSort: true,
            sorters: [{
                property: 'text',
                direction: 'ASC'
            }]
        });

        var tree = Ext.create('Ext.tree.Panel', {
            store: store,
            renderTo: 'tree_el',
            height: 300,
            width: 250,
            title: 'Eatables'
        });
    });

私の現在の結果は次のようになります

ここに画像の説明を入力

私の期待される結果は

ここに画像の説明を入力

データベースからデータを取得しているときに問題が発生しました。期待される形式が達成されるように修正してください。私のphpファイルには修正が必要だと思います。

4

2 に答える 2

0

extjs にフィードする必要があるツリーの生成中に論理エラーが発生しました。私はPHPを知らないので、javascriptを書きます:

function getTree()
{
    var tree, list = [], root, result = []; // fetch list from database
    tree = {text : root.name};
    result.push(createTreeStructur(tree, root, list));
    return result;
}
    //tree = holder of data, root = parent in each recursive call, list = complete list from database
    function createTreeStructure(tree, root, list) {
       var i=0, ln = children.length, result = [], child, childList = [], temptree = {};
       var children = getChildren(root, list); //Fetch children
       for(i=0, i<ln;i++)
       {
          child = children[i];
          tree = [];
          if(getChildren(child, list).length===0) //If no children of child exist
          {
             temptree = {name : child.name, leaf : true};
             childList.push(temptree);
             tree["children"] = childList; // Add child as child of the passed parent
          }
          else
          {
             temptree = {name : child.name, leaf : false}; 
             childList.push(temptree);
             tree["children"] = childList;
             createTreeStructure(temptree, child, list); // Recursively create tree structure for the child since children exist.
          }
       }
      result.push(tree);
      return result;
    }

    function getChildren(root, list)
    {
       var i=0, ln = list.length, result = [];
       for(i=0, i<ln;i++)
       {
          if(root.id===list[i].parent)
          {
             result.push(list[i]);
          }
       }
       return result;
    }
于 2012-11-23T15:28:55.550 に答える