【问题标题】:Prestashop, building the sub categories treePrestashop,构建子类别树
【发布时间】:2013-03-06 15:52:45
【问题描述】:

我正在尝试为 Prestashop 1.4 构建一个模块,我需要检索并显示完整的类别和子类别列表,但我发现了一些困难:

这是我的模块逻辑:

class HyperFeed extends Module {

 function __construct(){ blablabla } 

 public function install(){ blablabla } 

public function _getCategories(){

    $version_mask = explode('.', _PS_VERSION_, 3);
    if ($version_mask[1]<5){$id_category=1;}else{$id_category=0;}       

    function getCategories($id_category){

        $sql = 'SELECT '._DB_PREFIX_.'category.id_category,name,'._DB_PREFIX_.'category.level_depth,'._DB_PREFIX_.'category.id_parent FROM '._DB_PREFIX_.'category
        INNER JOIN '._DB_PREFIX_.'category_lang ON '._DB_PREFIX_.'category_lang.id_category = '._DB_PREFIX_.'category.id_category
         WHERE id_parent = '.$id_category.' AND id_lang = 3';

        $contentTable = '<table>';
        if ($results = Db::getInstance()->ExecuteS($sql)){
            foreach ($results as $row){


                $contentTable .= '   
                <tr>
                    <td width="3%"><input type="checkbox" name="footerBox[]" class="cmsBox" id="1_1" value="1_1"></td>
                    <td width="3%">'.$row['id_category'].'</td>
                    <td width="94%"><img style="vertical-align:middle;" src="../img/admin/lv1.gif" alt=""> &nbsp;
                    <label for="1_1" class="t"><b>'.$row['name'].'</b></label></td>
                </tr>';


                getCategories($row['id_category']);                      

            }
        }
        $contentTable .=  '</table>';


    }

    getCategories(1);
    $this->_html .= $contentTable;

} 

 public function getContent(){

$this->_html .='<form>blablabla';

$this->_getCategories();

$this->_html .='blablabla</form>';

}
    return $this->_html;
    }

我得到的只是一个“未定义的变量:contentTable”,我做错了什么??

提前致谢

【问题讨论】:

    标签: php tree categories prestashop


    【解决方案1】:

    您的$contentTable 是在getCategories() 中定义的,而不是_getCategories()。

    因此在以下几行中,它被认为是未定义的。

    getCategories(1);
    $this->_html .= $contentTable;
    

    您可以尝试执行以下操作:

    查找:

    $contentTable .=  '</table>';
    

    替换为:

    $contentTable .=  '</table>';
    return $contentTable;
    

    查找:

    getCategories(1);
    $this->_html .= $contentTable;
    

    替换为:

    $contentTable = getCategories(1);
    $this->_html .= $contentTable;
    

    这应该通过从 getCategories() 函数返回和分配 $contentTable 变量来正确定义 _getCategories() 函数中的变量。

    【讨论】:

    • 谢谢,这几乎成功了,现在我得到了结果,但只显示了第一级类别而不是任何单个子类别,后来通过回显显示了完整的树
    • 已解决:我不得不更改循环:这:getCategories($row['id_category']);为此: $contentTable .= getCategories($row['id_category']);现在一切正常
    【解决方案2】:

    我发现了别的东西

    public static function getCategoryTree($id_product,$id_lang){
                    $root = Category::getRootCategory();
                    $selected_cat = Product::getProductCategoriesFull($id_product, $id_lang);
                    $tab_root = array('id_category' => $root->id, 'name' => $root->name);
                    $helper = new Helper();
                    $category_tree = $helper->renderCategoryTree($tab_root, $selected_cat, 'categoryBox', false, true, array(), false, true);
                    return $category_tree;
                }
    

    $this->getCategoryTree(null,$id_lang); 并且您将类别树作为 prestashop 后台关联选项卡。 将 null 替换为 $id_product 并检查产品类别。 希望我会有所帮助 对不起我的英语不好我是法国人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2013-04-03
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多