21

自分のサイトに表示するために、ツリートラバーサルモデルで階層的に設定されたデータを<ul>に取得しようとしています。

これが私のコードです:

function getCats($) {
  // retrieve all children of $parent
  $query = "SELECT max(rght) as max from t_categories";
  $row = C_DB::fetchSingleRow($query);
  $max = $row["max"];
  $result ="<ul>";
  $query = "SELECT * from t_categories where lft >=0 and rght <= $max";
  if($rs = C_DB::fetchRecordset($query)){
    $p_right ="";
    $p_left ="";
    $p_diff="";          
    while($row = C_DB::fetchRow($rs)){
      $diff = $row["rght"] -$row["lft"];

      if($diff == $p_diff){
        $result.= "<li>".$row['title']."</li>";
      }elseif (($row["rght"] - $row["lft"] > 1) && ($row["rght"] > $p_right)){
        $result. "<ul>";
        $result.= "<li>".$row['title']."</li>";

      }else{
        $result.= "<li>".$row['title']."</li>";
      } 

      $p_right = $row["rght"];
      $p_left = $row["lft"];
      $p_diff = $diff;
    }
  }
  $result.= "</ul>";
  return $result;
} 

これが私のサンプルテーブルです:

|ID  |  TITLE | lft| rght |
|1   | Cat 1  | 1      |    16       |
|18  | Cat 2  | 3      |    4       |
|22  | Cat 3  | 5      |    6       |
|28  | Cat 4  | 7      |    8       |
|34  | Cat 5  | 9      |    9       |
|46  | Cat 6  | 11      |    10       |
|47  | Cat 7  | 13      |    12       |
|49  | Cat 8  | 15      |    14       | 

これで、次のように出力されます。

    <ul>
<li>Cat 1</li>
<li>Cat 2</li>
<li>Cat 3</li>
<li>Cat 4</li>
<li>Cat 5</li>
<li>Cat 6</li>
<li>Cat 7</li>
<li>Cat 8</li>
</ul>

階層構造でリストを出力する理由や方法を教えてもらえますか?

関連トピック

4

7 に答える 7

52

よし、賞金稼ぎをしよう ;)

ステップ 0 - サンプルのサニタイズ:
既に述べたように、有効なネストされたセットを定義していないため、サンプル データは壊れています。このデータをアプリから取得した場合は、挿入/削除ロジックを確認する必要があります。

したがって、テストのために、次のようなサニタイズされたバージョンを使用しました
:

CREATE TABLE t_categories`(
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(45) NOT NULL,
  `lft` INTEGER UNSIGNED NOT NULL,
  `rght` INTEGER UNSIGNED NOT NULL,
  PRIMARY KEY (`id`)
);

INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 1',1,16);
INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 2',2,3);
INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 3',4,7);
INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 4',5,6);
INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 5',8,13);
INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 6',9,12);
INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 7',10,11);
INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 8',14,15);

ステップ 1 - データベースに順序付けを行わせる
ネストされたセットは、主にデータベースにツリーを格納する便利な方法として発明されました。これにより、サブツリー、親関係、およびこの場合特に興味深い順序と深さのクエリが非常に簡単になるためです。

SELECT node.title, (COUNT(parent.title) - 1) AS depth
 FROM t_categories AS node
 CROSS JOIN t_categories AS parent
 WHERE node.lft BETWEEN parent.lft AND parent.rght
 GROUP BY node.title
 ORDER BY node.lft

これにより、ルート ノードから始まり最後まで順番に並べられたセットが返されます。最も重要なことは、各ノードの深さを正の整数として追加し、ノードがルート (レベル 0) の下にあるレベル数を示すことです。上記の例のデータの場合、結果は次のようになります。

title, depth
'Cat 1', 0
'Cat 2', 1
'Cat 3', 1
'Cat 4', 2
'Cat 5', 1
'Cat 6', 2
'Cat 7', 3
'Cat 8', 1

コード内:

// Grab ordered data
$query = '';
$query .= 'SELECT node.title, (COUNT(parent.title) - 1) AS depth';
$query .= ' FROM t_categories AS node';
$query .= ' CROSS JOIN t_categories AS parent';
$query .= ' WHERE node.lft BETWEEN parent.lft AND parent.rght';
$query .= ' GROUP BY node.title';
$query .= ' ORDER BY node.lft';

$result = mysql_query($query);

// Build array
$tree = array();
while ($row = mysql_fetch_assoc($result)) {
  $tree[] = $row;
}

結果の配列は次のようになります。

Array
(
    [0] => Array
        (
            [title] => Cat 1
            [depth] => 0
        )

    [1] => Array
        (
            [title] => Cat 2
            [depth] => 1
        )
    ...
)

ステップ 2 - HTML リスト フラグメントとして出力:

while ループの使用:

// bootstrap loop
$result = '';
$currDepth = -1;  // -1 to get the outer <ul>
while (!empty($tree)) {
  $currNode = array_shift($tree);
  // Level down?
  if ($currNode['depth'] > $currDepth) {
    // Yes, open <ul>
    $result .= '<ul>';
  }
  // Level up?
  if ($currNode['depth'] < $currDepth) {
    // Yes, close n open <ul>
    $result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
  }
  // Always add node
  $result .= '<li>' . $currNode['title'] . '</li>';
  // Adjust current depth
  $currDepth = $currNode['depth'];
  // Are we finished?
  if (empty($tree)) {
    // Yes, close n open <ul>
    $result .= str_repeat('</ul>', $currDepth + 1);
  }
}

print $result;

再帰関数と同じロジック:

function renderTree($tree, $currDepth = -1) {
  $currNode = array_shift($tree);
  $result = '';
  // Going down?
  if ($currNode['depth'] > $currDepth) {
    // Yes, prepend <ul>
    $result .= '<ul>';
  }
  // Going up?
  if ($currNode['depth'] < $currDepth) {
    // Yes, close n open <ul>
    $result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
  }
  // Always add the node
  $result .= '<li>' . $currNode['title'] . '</li>';
  // Anything left?
  if (!empty($tree)) {
    // Yes, recurse
    $result .=  renderTree($tree, $currNode['depth']);
  }
  else {
    // No, close remaining <ul>
    $result .= str_repeat('</ul>', $currNode['depth'] + 1);
  }
  return $result;
}

print renderTree($tree);

どちらも次の構造を出力します。

<ul>
    <li>Cat 1</li>
    <li>
        <ul>
            <li>Cat 2</li>
            <li>Cat 3</li>
            <li>
                <ul>
                    <li>Cat 4</li>
                </ul>
            </li>
            <li>Cat 5</li>
            <li>
                <ul>
                    <li>Cat 6</li>
                    <li>
                        <ul>
                            <li>Cat 7</li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>Cat 8</li>
        </ul>
    </li>
</ul>

ニトピッカー コーナー: 質問者は明示的に を要求しまし<ul>たが、順序付けられていないリスト!? さあ...
;-)

于 2009-08-26T21:43:41.923 に答える
16

Henrik Opelのものの代わりに私のために働いたより良いレンダリングツリー関数( jsTree jQueryプラグインで使用するためのhtmlソースを準備するためのphp関数):

function MyRenderTree ( $tree = array(array('name'=>'','depth'=>'')) ){

$current_depth = 0;
$counter = 0;

$result = '<ul>';

foreach($tree as $node){
    $node_depth = $node['depth'];
    $node_name = $node['name'];
    $node_id = $node['category_id'];

    if($node_depth == $current_depth){
        if($counter > 0) $result .= '</li>';            
    }
    elseif($node_depth > $current_depth){
        $result .= '<ul>';
        $current_depth = $current_depth + ($node_depth - $current_depth);
    }
    elseif($node_depth < $current_depth){
        $result .= str_repeat('</li></ul>',$current_depth - $node_depth).'</li>';
        $current_depth = $current_depth - ($current_depth - $node_depth);
    }
    $result .= '<li id="c'.$node_id.'"';
    $result .= $node_depth < 2 ?' class="open"':'';
    $result .= '><a href="#"><ins>&nbsp;</ins>'.$node_name.'</a>';
    ++$counter;
}
 $result .= str_repeat('</li></ul>',$node_depth).'</li>';

$result .= '</ul>';

return $result;}

結果のHTML:

<ul>
    <li id="c1" class="open"><a href="#"><ins>&nbsp;</ins>ELECTRONICS</a>
        <ul>
            <li id="c2" class="open"><a href="#"><ins>&nbsp;</ins>TELEVISIONS</a>
                <ul>
                    <li id="c3"><a href="#"><ins>&nbsp;</ins>TUBE</a></li>
                    <li id="c4"><a href="#"><ins>&nbsp;</ins>LCD</a></li>
                    <li id="c5"><a href="#"><ins>&nbsp;</ins>PLASMA</a>
                        <ul>
                            <li id="c14"><a href="#"><ins>&nbsp;</ins>PLASMA1</a></li>
                            <li id="c15"><a href="#"><ins>&nbsp;</ins>PLASMA2</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li id="c6" class="open"><a href="#"><ins>&nbsp;</ins>PORTABLE ELECTRONICS</a>
                <ul>
                    <li id="c7"><a href="#"><ins>&nbsp;</ins>MP3 PLAYERS</a>
                        <ul>
                            <li id="c8"><a href="#"><ins>&nbsp;</ins>FLASH</a></li>
                        </ul>
                    </li>
                    <li id="c9"><a href="#"><ins>&nbsp;</ins>CD PLAYERS</a></li>
                    <li id="c10"><a href="#"><ins>&nbsp;</ins>2 WAY RADIOS</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
于 2009-11-24T13:53:38.573 に答える
4

ネストされたセットを処理するための PEAR パッケージがあります: DB_NestedSetまた、MySQL での階層データの管理
に関する記事にも興味があるかもしれません。

于 2009-08-21T09:25:24.787 に答える
1

これはあなたが探しているものでなければなりません:

function getCats($left = null, $right = null)
{
    $sql = array();
    $result = null;

    if (isset($left) === true)
    {
        $sql[] = 'lft >= ' . intval($left);
    }

    if (isset($right) === true)
    {
        $sql[] = 'rght <= ' . intval($right);
    }

    if (empty($sql) === true)
    {
        $sql[] = 'lft = 1';
    }

    $sql = 'SELECT * FROM t_categories WHERE ' . implode(' AND ', $sql) . ';';

    if ($rs = C_DB::fetchRecordset($sql))
    {
        // you need to make sure that the query returns
        // something to correctly display the ULs
        if (empty($rs) === false)
        {
            $result .= '<ul>' . "\n";

            while ($row = C_DB::fetchRow($rs))
            {
                $result .= '<li>' . $row['title'] . '</li>' . "\n";
                $result .= getCats($row['lft'], $row['rght']);
            }

            $result .= '</ul>' . "\n";
        }
    }

    return $result;
}

ネストされたツリーのHTMLを取得するには、次のようにする必要があります。

echo getCats();

ネストされたセットのサンプルが正しく表示されないことに注意してください。また、C_DBクラスの呼び出しを間違えなかったかどうかを確認する必要があります。これは、よく知らないためわかりません。

于 2009-08-26T13:39:31.820 に答える
1

結果をループするだけで、次のようになります。

$sql = "SELECT node.name, (COUNT(parent.name) - 1) AS depth
        FROM nested_category AS node,
        nested_category AS parent
        WHERE node.lft BETWEEN parent.lft AND parent.rgt
        GROUP BY node.name
        ORDER BY node.lft";

$query_result = mysql_query($sql)

$result = "<ul>";
$currDepth = 0;

while($row = mysql_fetch_assoc($query_result))
{
  if($row['depth'] > $currDepth)
  {
    $result .= "<li><ul>"; // open sub tree if level up
  }

  if($row['depth'] < $currDepth)
  {
    $result .= str_repeat("</ul></li>", $currDepth - $row['depth']); // close sub tree if level down
  }

  $result .= "<li>$row['name']</li>"; // Always add node
  $currDepth = $row['depth'];
}
$result .= "</ul>";

echo $result;
于 2011-01-16T11:18:49.823 に答える
0
$linaje='';
    $lastnode='';
    $sides['izq']=array();
    $sides['der']=array();
    $print = '<ul>'; 
    foreach ($array as $key1 => $value1){ //Proyectos

        if(strpos($info[$key1]['linaje'],'-') !== false)
            $compare = strstr($info[$key1]['linaje'],'-',true);
        else
            $compare  = $info[$key1]['linaje'];

        if($linaje != ''){
            if ($linaje !=   $compare){
                $linaje= $compare;
                $sides['izq']=array();
                $sides['der']=array();
                //for($i=1;$i <= substr_count($lastnode,'`')-substr_count($value1,'`');$i++)
                    //$print .= '</ul></li>';
            }
        }


        if ($lastnode != '')
            for($i=1;$i<= substr_count($lastnode,'`')-substr_count($value1,'`');$i++)
                $print .= '</ul></li>'; 

        if (count($sides['der'])>0)
            if  ($sides['der'][count($sides['der'])-1] > $info[$key1]['der'])
                $print .= '<ul>';

        $print .= '<li><a href="#'.$info[$key1]['id'].'#'.$info[$key1]['linaje'].'">'.substr($value1,substr_count($value1,'`')).'</a>';

        if  ($info[$key1]['der'] - $info[$key1]['izq'] == 1)
                $print .= '</li>';

        if ($key1 == count($info)-1)
            for($i=1;$i <= substr_count($lastnode,'`')-1;$i++)
                $print .= '</ul></li>';

        $sides['der'][] = $info[$key1]['der'];
        $sides['izq'][] = $info[$key1]['izq'];

        if ($linaje =='')
                $linaje = $info[$key1]['linaje'];

        $lastnode = $value1;
    }
    $print .= '</ul>';
    echo $print;

これの違いは、X 本の木をレンダリングできることです。これは、私のプロジェクトの 1 つに当てはまります。DBから行をフェッチするときに、深さ参照としてcharを使用します

于 2010-08-02T17:39:59.620 に答える
0

私はjsTree jQueryメニューを表示するCROSS JOINクエリを使用しています。すべてがうまく機能します!既存のテーブルに位置の列を追加しました。ただし、位置を定義してすべてを位置順に並べ替えると、対応するアイテムが適切にグループ化されません。クエリの問題だと思います。多くの組み合わせを試しましたが、成功しませんでした。

于 2011-11-03T10:38:10.750 に答える