2012-01-10 2 views
0

私はInterspireショッピングカートと私の厄介なコーディングスキルをもう一度出して戦っています。 :)このPHP関数がすべての可変データを渡さないのはなぜですか?

私の目標は、BHphotovideo.comのフロントページにあるカテゴリブロックに似たカテゴリリストを作成することです(高額なのですか?)。私はこれが無料のショッピングカートで提供される機能だと思っていますが、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 
?> 

は、多くの(私の最後ですので、リンクがクリックできますし、リストがパネルである私は空白のPHPファイルに貼り付けたときに以下のコードは素晴らしい作品が、私はISCにこれを統合する必要があります)は、このコードをスタンドアロンのISCパネルとして統合しようとします。私はちょうどこれと一緒にどこに行くのか分からない。注意:未定義の変数:childrenTree in /includes/display/HomeCategoryList.php on line 31

しかし、childrenTreeは_getcats関数で$ categorynamesとして定義されています私はそれが$ categorynamesのデータを渡したが、$ 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

私がコードの2番目の部分で目にしている明らかな問題は、$ childrenTreeと$ categoryNamesを定義した方法です。それらを_getcats()でローカルに定義した後、renderTree()から呼び出すことができます。 _getcats()を変更して2つ(ツリー/名前)を含む新しい配列を返すか、クラス内でプライベート宣言して、そうするように呼び出す必要があります。

すなわち

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

ところで上記のコードスニペットは、あなたのコーディングスタイル(StackOverflowの上で自分のコードを貼り付けとしていない問題)である場合は、おそらくそれを変更する必要があります。

関連する問題