2012-01-10 31 views
0

我正在與Interspire購物車以及我狡猾的編碼技巧再次交鋒。 :)爲什麼這個PHP函數不能傳遞所有的變量數據?

我的目標是創建一個類似於BHphotovideo.com首頁上類別塊的類別列表(崇高?)。 :)我相信這是一款即使免費購物車也能提供的功能,但不會在ISC中預建。我只想要一個包含父類別下的子類別的所有頂級類別的可點擊列表。下面的代碼的偉大工程,當我把它粘貼到空白的PHP文件,但我需要將此融入ISC所以鏈接的點擊能和上市是面板:

<?php 
// Make a MySQL Connection 
$cn = mysql_connect("localhost", "mydbuser", "password") or die(mysql_error()); 
mysql_select_db("mydb") or die(mysql_error()); 

$rs = mysql_query("SELECT categoryid, catparentid, catname FROM isc_categories", $cn) 
or die(mysql_error()); 

    $childrenTree = array(); //Will store an array of children for each parent 
    $categoryNames = array(); //Will store category name for each id 

//We fill $childrenTree and $categoryNames from database 
while($row = mysql_fetch_array($rs)){ 
list($id, $parent_id, $category) = $row;  
$categoryNames[(string)$id] = $category; 
$parent_id = (string)$parent_id; 
if(!array_key_exists($parent_id, $childrenTree)) 
    $childrenTree[$parent_id] = array(); 
$childrenTree[$parent_id][] = (string)$id; 
} 


//Main recursive function. I'll asume '0' id is the root node 
function renderTree($parent = "0"){ 
global $categoryNames; 
global $childrenTree; 
if($parent != "0") echo "<li> ", $categoryNames[$parent], "\n"; 
$children = $childrenTree[$parent]; 
if(count($children) > 0){ //If node has children 
    echo "<ul>\n"; 
    foreach($children as $child) 
     renderTree($child); 
    echo "</ul>\n"; 
} 
if($parent != "0") echo "</li>\n"; 
} 
renderTree(); //This renders the hierarchical tree 
?> 

下面是我的最後一個(很多)試圖將這些代碼集成爲一個獨立的ISC面板。我只是不知道該去哪裏。我用下面的代碼得到的錯誤是:注意:未定義的變量:第31行的/includes/display/HomeCategoryList.php中的childrenTree

但是,childrenTree在_getcats函數中被定義爲$ categorynames,腳本不會沒有抱怨,所以我會認爲它將$ categorynames的數據傳遞給renderTree函數,而不是$ childrenTress。它是否正確?

對於原始代碼,函數_getcats不存在,並且不是必需的,但是下面將它添加到面板的腳本迫使我將該代碼放入函數中。另外,如果我將數據庫查詢語法更改爲與其他ISC文件中通常使用的語法相匹配,那麼該腳本會抱怨此行的未定義變量:list($ id,$ parent_id,$ category)= $ row。我不知道爲什麼當查詢應該返回相同的結果。

<?php 

CLASS ISC_HOMECATEGORYLIST_PANEL extends PANEL 
{ 
    public function SetPanelSettings() 
    { 
    $GLOBALS['SideCategoryListTypeClass'] = 'SideCategoryListClassic'; 
    $GLOBALS['SNIPPETS']['HomeCategoryList'] = $this->renderTree(); 
    } 

    function _getcats(){ 
    $rs = mysql_query("SELECT categoryid, catparentid, catname FROM isc_categories") 
      or die(mysql_error()); 

     $childrenTree = array(); //Will store an array of children for each parent 
     $categoryNames = array(); //Will store category name for each id 

     while($row = mysql_fetch_array($rs)){ 
       list($id, $parent_id, $category) = $row;  
       $categoryNames[(string)$id] = $category; 
       $parent_id = (string)$parent_id; 
       if(!array_key_exists($parent_id, $childrenTree)) 
       $childrenTree[$parent_id] = array(); 
       $childrenTree[$parent_id][] = (string)$id; 
       } 
       } 

    function renderTree($parent = "0"){ 
      $this->_getcats(); 
     if($parent != "0")echo "<li> ", $categoryNames[$parent], "\n"; 
     $children = $childrenTree[$parent]; 
     if(count($children) > 0){ //If node has children 
      echo "<ul>\n"; 
      foreach($children as $child) 
       renderTree($child); 
      echo "</ul>\n"; 
     } 
     if($parent != "0") echo "</li>\n"; 
     } 

     } 

如果您發現任何問題,我忽略了或認爲您可能知道問題所在,請指出正確的方向。我已經呆了好幾天了。 :)

謝謝!

回答

1

我在代碼的第二部分看到的明顯問題是您定義$ childrenTree和$ categoryNames的方式。你可以在本地在_getcats()中定義它們,但是你可以從renderTree()中調用它們。您應該更改_getcats()以返回包含兩個(樹/名稱)的新數組,或者在類中聲明它們是私有的,並像這樣調用它們。

CLASS ISC_HOMECATEGORYLIST_PANEL extends PANEL 
{ 
    private $categoryNames = array(); 
    private $categoryTree = array(); 

    private function _getCats() { 
     ... 
     $this->categoryNames[(string)$id] = $category; 
     ... 
     if(!array_key_exists($parent_id, $this->childrenTree)) 
     $this->childrenTree[$parent_id] = array(); 

     $this->childrenTree[$parent_id][] = (string)$id; 
     ... 
    } 

    public function renderTree($parent = "0") { 
     // Call childrenTree/categoryNames by using the $this directive again 
    } 
} 

順便說一句,如果上面的代碼片段是你的編碼風格(而不是一個對計算器粘貼代碼的問題),你應該改變它。

相關問題