2012-01-10 69 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 
    } 
} 

顺便说一句,如果上面的代码片段是你的编码风格(而不是一个对计算器粘贴代码的问题),你应该改变它。