使用 smarty 和 php 创建类别树

Creating a category tree with smarty and php

我的网络项目中有类别和子类别。我是 smarty 的新手。 通常我使用下面的代码来构建类别树。主要类别由 parent id = 0 定义,所有子类别都将主要类别 id 作为其父 id

$sql = "SELECT * FROM categories ORDER BY category";
$res = $mysqli->query($sql);
while ($row = mysqli_fetch_assoc($res)) {
    $parent = intval($row['parent_id']);
    if (!isset($categories[$parent])) {
        $categories[$parent] = array();
    }
    $categories[$parent][] = $row;
}
function build_categories($parent, $categories) {
    if (isset($categories[$parent]) && count($categories[$parent])) {
             foreach ($categories[$parent] as $category) {

             $cat_name = $category['category'];
             $cat_link = preg_replace("![^a-z0-9]+!i", "-", $cat_name);
             $cat_link = urlencode($cat_link);
             $cat_link = strtolower($cat_link);

             if($category['parent_id']==0){
              echo '<li><a href="category-'.$category['cat_id'].'-'.$cat_link.'">' .$category['category'].'</a>';
             }else{
              echo '<li><a href="subcategory-'.$category['cat_id'].'-'.$cat_link.'"><span class="fa fa-angle-double-right"></span> '.$category['category'].'</a>';       
             }
            echo build_categories($category['cat_id'], $categories);
            echo '</li>';
        }


    }

}   

//output category tree  
build_categories(0, $categories);   

//table结构

cat_id | category | parent_id
1          | cat 1        |  0  
1          | cat 2        |  0  
1          | cat 3        |  1  
1          | cat 4        |  2  
1          | cat 5        |  1

请注意,上面的代码在我为其分配 smarty 变量时不起作用。

但我似乎无法让它与 Smarty 一起工作。申请你的帮助。

试试这个。在我的本地机器上运行良好。

require_once('smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->setTemplateDir('tpl');
$smarty->setCompileDir('tpl_c');
$mysqli = new mysqli('localhost', 'root', '', 'test');

$sql = "SELECT * FROM categories ORDER BY category";
$res = $mysqli->query($sql);
while ($row = mysqli_fetch_assoc($res)) {
    $parent = intval($row['parent_id']);
    if (!isset($categories[$parent])) {
        $categories[$parent] = array();
    }
    $categories[$parent][] = $row;
}
function build_categories($parent, $categories) {
    if (isset($categories[$parent]) && count($categories[$parent])) {
         foreach ($categories[$parent] as $category) {
             $cat_name = $category['category'];
             $cat_link = preg_replace("![^a-z0-9]+!i", "-", $cat_name);
             $cat_link = urlencode($cat_link);
             $cat_link = strtolower($cat_link);

             if($category['parent_id']==0){
              $result[] = '<li><a href="category-'.$category['cat_id'].'-'.$cat_link.'">' .$category['category'].'</a>';
             }else{
               $result[] =  '<li><a href="subcategory-'.$category['cat_id'].'-'.$cat_link.'"><span class="fa fa-angle-double-right"></span> '.$category['category'].'</a>';
             }
             $result[] =  build_categories($category['cat_id'], $categories);
             $result[] =  '</li>';
             return implode('', $result);
        }
    }
}

$smarty->assign('smarty_var', build_categories(0, $categories));
$smarty->display('template.tpl');

编辑: 添加了 table 脚本中使用的结构

 id| cat_id| category    | parent_id    
 1 | 1     | cat 1 main  | 0
 2 | 2     | cat 2 child | 1

编辑: 更新函数

function build_categories($parent, $categories) {
         foreach ($categories[$parent] as $category) {
             $cat_name = $category['category'];
             $cat_link = preg_replace("![^a-z0-9]+!i", "-", $cat_name);
             $cat_link = urlencode($cat_link);
             $cat_link = strtolower($cat_link);

             if($category['parent_id']==0){
               $result[] = '<li><a href="category-'.$category['cat_id'].'-'.$cat_link.'">' .$category['category'].'</a>';
             }else{
               $result[] =  '<li><a href="subcategory-'.$category['cat_id'].'-'.$cat_link.'"><span class="fa fa-angle-double-right"></span> '.$category['category'].'</a>';
             }
             if(isset($categories[$category['cat_id']])) {
                 $result[] =  build_categories($category['cat_id'], $categories);
             }
             $result[] =  '</li>';
        }
        return implode('', $result);
}