0

データベースには、次のようなネストされたセットモデルを使用して取得しているいくつかのカテゴリがあります(括弧内の数字は深さを表します):

新規(1)
-一般(2) -
コンソール(2)

ゲーム(1)
-pc(2)
--エミュレーター(3)
-ps3(2)

次に、多次元配列を処理し、ナビゲーション メニューを生成する関数があります。 これは、データベースから次の配列を返す

モデルです。

Array (
    [0] => Array ( [name] => News [depth] => 1 ) 
    [1] => Array ( [name] => General [depth] => 2 ) 
    [2] => Array ( [name] => Console [depth] => 2 ) 
    [4] => Array ( [name] => Games [depth] => 1 ) 
    [5] => Array ( [name] => PC [depth] => 2 ) 
    [6] => Array ( [name] => emulator [depth] => 3 ) 
    [8] => Array ( [name] => ps3 [depth] => 2 ) 
) 

-- コントローラー

public function index()
{
$navTree = $this->getNavTree(); //gets array from model
$createNavTree = $this->_renderTree($navTree); //pass array to function
$this->load->view('testnavigation.php', $createNavTree);
}


function _renderTree($tree){    
    $current_depth = 0;
    $counter = 0;

    $result = '';

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

        if($node_depth == $current_depth){
            if($counter > 0) $result .= '</li>';            
        }
        elseif($node_depth > $current_depth){

            $result .= $counter == 0 ? '<ul id="nav">' : '<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><a href="#">'.$node_name.'</a>';
        ++$counter;
    }
    $result .= str_repeat('</li></ul>',$node_depth).'</li>';
    $result .= '</ul>';     
    return $result;
}

- 見る

echo $createNavTree;

リストはインデントされた正しい順序で作成されますが、私が抱えている問題は、各アイテムのリンクを生成することです。たとえば、エミュレーターのリンクは mysite.com/games/pc/emulator にする必要があります

どうすればこれを達成できますか?

4

1 に答える 1

0

簡単なルートを使用して、キャッシュレベルの配列と各レベルの以前に計算されたパスを作成します。の「ベースレベル」が1再び表示されたら、キャッシュをクリアして、無効なエントリがないようにします。

このサンプルコードは、現在使用しているもので機能するはずです(このコードを現在のレイアウトレンダリングコードの上に配置$pathし、URLとして使用するだけです)。

function _renderTree($tree) {
    $depths = array();
    foreach ($tree as $node) {
        // build the current path
        $path = (($node['depth'] > 1) ? $depths[$node['depth'] - 1] : '') . $node['name'];

        // set the current path as the current depth's path (to be used for any deeper-nodes)
        $depths[$node['depth']] = $path . '/';

        ... layout rendering code ...
    }
}

現在のパスdepthが1より大きい場合(つまり、親の深さが存在する場合)、親の計算されたパス(親の名前だけでなく、親のフルパスである必要があります)を使用します次に、現在の名前を追加します。これにより、完全なネストされたパスが得られます。次に、生成されたパスを$depths(現在の深度でインデックス付けされた)配列に格納して、子が使用できるようにします。

サンプルでは、​​パスを指定Emulatormysite.com/games/pc/emulator、小文字とドメイン名を強調します。パスでを使用しているので$node['name']、次を使用することをお勧めしますstrtolower()

$path = (($node['depth'] > 1) ? $depths[$node['depth'] - 1] : '') . strtolower($node['name']);

<a></a>タグの実際の行にドメイン名を追加することをお勧めします。

$result .= '<li><a href="http://mysite.com/' . $path . '">'.$node_name.'</a>';
于 2012-10-08T12:41:58.650 に答える