PHP无限级分类的简单实现方法

飘逸的风13年前 (2013-01-26)程序6585

PHP无限级分类的简单实现方法,请看截图:

 

代码是:

 <?php 
header("Content-type: text/html; charset=utf-8"); 
$cats = array( 
    array( 
        'id' => 1, 
        'name' => '学术和教育', 
        'children' => array( 
            array( 
                'id' => 1, 
                'name' => '自然科学', 
                'children' => null, 
            ), 
            array( 
                'id' => 1, 
                'name' => '人文科学', 
                'children' => null, 
            ), 
            array( 
                'id' => 1, 
                'name' => '语言科学', 
                'children' => array( 
                    array( 
                         'id' => 1, 
                         'name' => '小学', 
                         'children' => null, 
                    ), 
                    array( 
                         'id' => 1, 
                         'name' => '中学', 
                         'children' => null, 
                    ) 
                ), 
            ) 
        ) 
    ), 
    array( 
        'id' => 1, 
        'name' => '生活', 
        'children' => array( 
            array( 
                'id' => 1, 
                'name' => '二手出租', 
                'children' => null, 
            ), 
            array( 
                'id' => 1, 
                'name' => '充值卡', 
                'children' => null, 
            ), 
            array( 
                'id' => 1, 
                'name' => '求职信息', 
                'children' => null, 
            ) 
        ) 
    ) 
); 

echo '<div id="Box">'; 
getTree($cats); 
echo '</div>'; 

function getTree($array) 

    echo '<ul>'; 
    foreach($array as $key=>$items) 
    { 
        if($items['children']!=null){ 
            echo '<li class="dir">'.$items['name'].'</li>'; 
            getTree($items['children']); 
        } 
        else 
            echo '<li>'.$items['name'].'</li>'; 
    } 
    echo '</ul>'; 



?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="https://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无限级分类</title> 
<style type="text/css"> 
<!-- 
body,td,th { 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 12px; 

body { 
    margin-left: 0px; 
    margin-top: 0px; 
    margin-right: 0px; 
    margin-bottom: 0px; 

#Box{ 
    width:400px; 
    margin:100px; 
    border-bottom:1px solid #cccccc; 

ul{ 
    cursor:pointer; 
    margin:0px; 
    padding:0px; 
    list-style:none; 

ul li{ 
    padding:5px 0px 5px 20px; 
    font-size:14px; 
    background-color:#EDF7FF; 
    border:1px solid #cccccc; 
    border-bottom:none; 

ul  ul { 
        display:none; 

ul  ul li{ 
    background-color:#FFF; 

.dir{ 
    font-weight:bold; 
    background:url(https://demo1.saitseo.com/detect/images/l.jpg) no-repeat 10px 10px #EDF7FF; 
     

ul  ul li.dir{ 
    background-color:#FFFFFF; 



--> 
</style> 
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script language="javascript"> 
$(function(){ 
    $('.dir').click(function(){ 
        if($(this).next().css('display')=='none'){ 
            $(this).next().slideDown(300); 
            $(this).css({'backgroundImage':'url(https://demo1.saitseo.com/detect/images/d.jpg)','backgroundPosition':'8px 10px'}); 
        }else 
        { 
            $(this).next().slideUp(300); 
            $(this).css({'backgroundImage':'url(https://demo1.saitseo.com/detect/images/l.jpg)','backgroundPosition':'10px 10px'}); 
        } 
    });            
}); 
</script> 
</head> 

<body>

原文出处:https://www.ideawu.net/blog/archives/585.html 
</body> 
</html>

相关文章

php正则取得网页上所有的link链接

$con=  file_get_contents($url); $pattern = '/<a(?:.*?)href="(((?:http(?:s?):\/\/)?([^\"\/]+...

分享一个生成sitemap.xml的类

很多站长在做seo优化的时候都会向各大搜索引擎网站提交站点地图sitemap.xml,同样需要提交。于是专门写了一个生成sitemap.xml的类,支持生成在制定目录下,默认生成在网站根目录下。 分...

Javascript 正则表达式使用手册

一.正则表达式匹配常用语法 “+”字符:规定表达式字符出现一次或多次。 “*”字符:规定表达式字符出现零次或多次。 “?”字符:规定表达式字符出现零次或一次。 ^ 匹配的是字符的开头 ,匹配的...

ECShop设置模板的原理以及一些模板设置函数说明(个人观点)

ECShop设置模板的原理以及一些模板设置函数说明(个人观点) admin/includes/lib_template.php 一些函数及变量说明 $template_files...

PHP攻击网站防御代码-以及攻击代码反译

这是我无意中攻击一个网站发现的一个代码PHP有效的拦截住我的DDOS  <?php   //查询禁止IP   $ip =$_SERVER['R...

thinkphp 使用U方法自动生成URL超链接

ThinkPHP U方法 U 方法是 ThinkPHP 内置的一个快捷方法,可以根据系统 URL 模式配置动态的生成智能的 URL 地址。 由于 ThinkPHP 支持各种不同的 URL...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。