2012-10-29 71 views
0

我試圖建立一個導航,當用戶選擇一個類別,那麼導航將只顯示所選類別的子類別。如何顯示所選主要類別的子類別?

我是從URL得到一個變量傳遞作爲父ID,它看起來像這樣:

locolhost/store.php?c=2 

我要找的應該像導航:

Parent 
    child 
    child 
Parent 
Parent 
Parent 

但是,目前我的代碼產生:

Parent 
    child 
    child 
Parent 
    child 
    child 
Parent 
    child 
    child 
Parent 
    child 
    child 

這裏是我當前的代碼:

shop.php

$parent_id = $_GET['p']; 
include('navigation.php'); 
$navigation = new navigation; 
print($navigation->main($parent_id)); 

navigation.php

public function main($parent) 
{ 
    /* PDO */ 
    $array = $categoryVIEW->fetchAll(PDO::FETCH_ASSOC); 
    return $this->buildNav($array,$parent); 
} 
private function buildNav($array,$parent) 
{ 
    $html = ''; 
    foreach($array as $item) 
    { 
     if($item['parent'] === NULL) 
     { 
      $html .= "\n<div class=\"parent\"><a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a></div>\n"; 
      $html .= "<div class=\"child\">\n"; 
      $html .= $this->getChildren($array,$parent); 
      $html .= "</div>\n"; 
     } 
    } 
    return $html; 
} 
private function getChildren($array,$parent) 
{ 
    $html = ''; 
    foreach($array as $item) 
    { 
     if($item['parent']===$parent) 
     { 
      $html .= "\t<a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a>\n"; 
     } 
    } 
    return $html; 
} 

我只是簡單地調用名爲getChildren()buildNav()這得到所有選定類別的兒童。我想我需要一個條件,只有當我想要顯示它的孩子的父母正在經歷循環時,纔會撥打getChildren() ...如果這有意義嗎?

這裏是我的數據庫:

enter image description here

+0

我很困惑與你的標題和'curently我越來越:'部分 – Deepak

+0

很抱歉的混亂,我的意思是,當我運行當前的代碼是輸出。 – Mike

+0

你可以打印'parentid'以及你的輸出..仍然我無法弄清楚什麼是你的問題! – Deepak

回答

0

我想通了......我需要添加一個條件。這裏的工作代碼:

private function buildNav($array,$parent) 
{ 
    $html = ''; 
    foreach($array as $item) 
    { 
     if($item['parent'] === NULL) 
     { 
      $html .= "\n<div class=\"parent\"><a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a></div>\n"; 
      $html .= "<div class=\"child\">\n"; 

      /* had to add this condition */ 
      if($item['category_id'] === $parent) 
      { 
       $html .= $this->getChildren($array,$parent); 
      }    
      $html .= "</div>\n"; 
     } 
    } 
    return $html; 
} 
private function getChildren($array,$parent) 
{ 
    $html = ''; 
    foreach($array as $item) 
    { 
     if($item['parent'] === $parent) 
     { 
      $html .= "\t<a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a>\n"; 
     }   
    } 
    return $html; 
} 
1

我不認爲你正在傳遞正確的「父」變量到子功能。請嘗試以下操作:

private function buildNav($array,$parent) 
{ 
    $html = ''; 
    foreach($array as $item) 
    { 
     if($item['parent'] === NULL) 
     { 
      $html .= "\n<div class=\"parent\"><a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a></div>\n"; 
      $html .= "<div class=\"child\">\n"; 
      // the following line needs to be changed 
      $html .= $this->getChildren($array,$item['category_id']); 
      $html .= "</div>\n"; 
     } 
    } 
    return $html; 
} 
+0

看到你的例子後,我發現了一些可能有用的東西。目前我有'$ this-> buildNav($ array,NULL)'我在想我應該調用我想看到的孩子的父母id。所以例如'buildNav($ array,2)' – Mike

+0

你的例子仍然顯示所有父母的孩子:( – Mike

+0

嗯,你確定嗎?看看這個例子它的作品。(注意我做了一些改變,所以它會工作在你的對象/類之外)http://codepad.org/euF9VLoR – Hank

相關問題