dongtan1009 2016-12-14 07:11
浏览 33
已采纳

用smarty和php创建一个类别树

I have categories and subcategories in my web project. I’m new to smarty. Normally I use below code to build a category tree. Main categories are defined by parent id = 0 and all the subcategories have main category id as their parent 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 structure

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

please note that above code work just dont work when i assign it smarty variable.

But i cannot seems to get it to work with Smarty. appricate your help.

  • 写回答

1条回答 默认 最新

  • douxiong2001 2016-12-14 08:18
    关注

    Try this. On my local machine it works well.

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

    EDIT: added table structure used in script

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

    EDIT: updated function

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?