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

飘逸的风14年前 (2013-01-26)程序6704

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>

相关文章

1亿条数据在PHP中实现Mysql数据库分表100张

当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度。笔者做了一个简单的尝试,1亿条数据,分100张表。具体实现过程如下。 首先创建100张表: 英文代码 $i=0; whi...

提高PHP编程效率的53个要点

用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说...

文章内链的实现方法

在很多的时候,我在浏览其他的网站发现文章中一写关键词都被加上了链接链向对应的页面,我就在想他们是怎么实现这个功能的?为什么这么做,这么做有什么好处?   当然有什么好处一看便知,无论是从...

MemCache安装使用

Windows下的Memcache安装: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c:\me...

Emlog常用日志列表页的判断

  Emlog 用得久了,emer 们就难免会折腾一下自己的模板,谁让 Emlog 这么简单实用呢?除了折腾外观及各种特效,当然还有实用的功能,日志列表页的判断应该就属于此类。我们可以根据日志列表页面...

PHP购物车类,简单易用,移植CodeIgniter,并进行一些优化[附带实例]

个人感觉CodeIgniter用起来方便,但有时候做电子商城网站的时候,没有使用CodeIgniter框架,但想用CodeIgniter里的购物车程序,那么就需要对其购物车类进行修改。但CodeIgn...

发表评论

访客

看不清,换一张

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