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

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

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解析html

注:分析html的好东西 最近想用php写一个爬虫,就需要解析html,在sourceforge上找到一个项目叫做PHP Simple HTML DOM Parser,它可以以类似jQuery的...

PHP制作安装程序的原理与步骤详细讲解

本文讲解PHP制作安装程序的原理与步骤 1、制作PHP安装程序的原理         PHP程序的安装原理就是将数据库结构和内容导入到相...

用PHP判断一个gif图片是不是动画

01 <?php 02 function IsAnimatedGif($filename) 03 { 04  &...

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

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

分享一个生成sitemap.xml的类

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

ECShop仿淘宝商品详细页 实现尺码颜色关联显示库存

ECShop仿淘宝商品详细页 实现尺码颜色关联显示库存

  ecshop服装商城很多都十分需要一个颜色尺码的功能,其实在淘宝,凡客上都类似的功能,客户在下单时选容易选择相应的颜色尺码,其实ecshop要实现这样的功能并不难,因为ecshop是开源免费的。...

发表评论

访客

看不清,换一张

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