我再次与 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 面板。我只是不知道还能去哪里。我在下面的代码中遇到的错误是:注意:未定义变量:第 31 行 /includes/display/HomeCategoryList.php 中的 childrenTree
但是 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";
}
}
如果您立即看到任何东西,我忽略了或认为您可能知道问题所在,请指出我正确的方向。我已经在这几天了。:)
谢谢!