2012-01-10 3 views
0

나는 Interspire 장바구니와 나의 dodgy 코딩 기술로 다시 한번 진압하고 있습니다. :)이 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 패널로 통합하려고 시도합니다. 나는 이걸로 어디 가야할지 모르겠다. 주의 : 정의되지 않은 변수 : childrenTree in /includes/display/HomeCategoryList.php on line 31

그러나 childrenTree는 _getcats 함수에서 $ categorynames와 같이 스크립트가 정의하지 않습니다 불만이 없으므로 $ categoryname에 대한 데이터를 전달했지만 $ childrenTress는 renderTree 함수에 전달하지 않았다고 생각합니다. 이 올바른지?

원래 코드의 경우 _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()를 변경하여 두 개의 트리 (트리/이름)가 포함 된 새 배열을 반환하거나 클래스에서 private로 선언하고 그렇게 호출해야합니다.

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 
    } 
} 

Btw는 위의 코드 조각이 당신의 코딩 스타일 (유래에 코드를 붙여와 아닌 문제) 인 경우 당신은 아마 그것을 변경해야합니다.

관련 문제