0

私は2つのテーブルレポートを持っています.employee_detailsレポートには、employee_detailsテーブルのemp_idであるsuprvisor_id、subordinate_idフィールドが含まれています.レポートテーブルには3つのレベル(スーパーバイザー->部下->部下->従業員)が含まれており、名前を取得してドロップダウンとしてこのデータを表示したいemployee_detailsテーブルから、階層として.だから私を助けてくださいそれを行う方法はありますか?

4

1 に答える 1

0

穴ツリーを表示したい場合は、穴ツリーを配列で取得する必要があります。そして、これは少しトリッキーです。

ツリーにいくつのレベルがあるか本当にわかっていないと思います。したがって、最も簡単な方法は次のとおりですが、大きなツリーではパフォーマンスが低下します。ツリーが本当に大きくなったら、他のテクニックをチェックする必要があります。

以下は簡単に書かれたもので、テストされておらず、バグがある可能性があります。しかし、可能な解決策を示すために投稿します。

<?php

class tree {

    private $level = 0;

    public function getChildsRecoursive($parentid=0,$recoursive=false) {
        // you would have your own db object... please replace this...
        $db->select("select * from reporting r JOIN employee_details d ON(r.subordinate_id=d.emp_id) where supervisor_id='$parentid' ORDER BY d.name");
        $r = array();
        while($data = $db->fetchArray()) {
            $sid = $data['supervisor_id'];
            $cid = $data['subordinate_id'];
            if($recoursive) {
                $this->level++;
                $data['level'] = $this->level;
                $data['childs'] = $this->getChildsRecoursive($cid, true);
                $this->level--;
            }
            $r[] = $data;
        }
        return $r;
    }

    public function getDropdown() {
            // I suspect the top Level have a supervisor_id = 0
        $data = $this->getChildsRecoursive(0,true);
            // you can do a print_r($data) here to see if the results are correct

        $r = "<select>";
        foreach($data as $d) {
            $r .= $this->getOptionsRecoursive($d);
        }
        $r .= "</select>";
            return $r;
    }

    public function getOptionsRecoursive($data) {
          $r = "<option>";
          for($i=0;$i<$data['level'];$i++) {
            $r .= "&nbsp;";
          }
          $r .= $data['name'];
          $r .= "</option>\n";

          if(isset($data['childs'])) {
            foreach($data['childs'] as $c) {
                $r .= $this->getOptionsRecoursive($c);
            }
          }
          return $r;
    }

}
    ?>

理解に役立つことを願っています。(ツリーの大きさによっては、これにより多くのクエリが発生することを認識してください)。

開始するには、行う必要があります

$tree = new tree();
echo $tree->getDropdown();
于 2013-05-28T05:24:35.850 に答える