用smarty和php创建一个类别树

时间:2016-12-14 07:11:13

标签: php html mysql smarty3

我的网络项目中有类别和子类别。我是聪明的新手。 通常我使用下面的代码来构建一个类别树。主要类别由父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);   

//表格结构

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合作。请帮助你。

1 个答案:

答案 0 :(得分:1)

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

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');

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

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