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

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

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?):\/\/)?([^\"\/]+...

Javascript 正则表达式使用手册

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

php日历的高效写法

标题写的吸引人了些,不过我就觉得是挺高效的,我相信 之前看到过 irlvirus 写的 《php练习代码-日历》,我觉得效率都不是很好。 date()函数结合mktime() 可以得到一切东西...

PHP给图片添加水印和生成缩略图函数

给图片添加水印: /************************************ 函数: watermark($bigimg, $smallimg, $coord = 1) 作用:...

PHP 文件上传$_FILES

文件上传表单<form enctype="multipart/form-data" action="URL" method="post">  <inp...

可以让PHP编程事半功倍的类库

可以让PHP编程事半功倍的类库

在用php开发网站的时候,使用面向对象的方法确实可以提高代码复用率,减少代码冗余。而对初学者更友好的是,PHP开发网站所需要的大部分类库,网上都有十分优秀的类库存在了。作为一个程序猿当然不能重复制造轮...

发表评论

访客

看不清,换一张

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