0

Interspire のショッピング カートと、危険なコーディング スキルでまたもや格闘しています。:)

私の目標は、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 パネルとして統合するための私の (多くの) 最後の試みです。私はこれで他にどこに行くべきかわかりません。以下のコードで得られるエラーは次のとおりです: Notice: Undefined variable: childrenTree in /includes/display/HomeCategoryList.php on line 31

しかし、childrenTree は、スクリプトが文句を言わない $categorynames と同様に _getcats 関数で定義されているため、$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";
        }

        }

すぐに何かが見えた場合は、私が見落としているか、問題が何であるかを知っていると思われる場合は、正しい方向に向けてください. 私は何日もこれにいました。:)

ありがとう!

4

1 に答える 1

1

コードの 2 番目の部分で目にする明らかな問題は、$childrenTree と $categoryNames の定義方法にあります。_getcats() でローカルに定義しますが、renderTree() から呼び出します。2 つ (ツリー/名前) を含む新しい配列を返すように _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
    }
}

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

于 2012-01-10T18:31:07.150 に答える