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

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

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 soap带验证

Server端 <?php class Server { private $authenticated = false;   public function auth($...

PHP常用正则表达式汇总

1.    平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2.    &quo...

php日历的高效写法

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

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

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

ecshop搜索热门关键字的调用

码关键字的设定在:后台-商店设置-显示设置--首页搜索的关键词 {if $searchkeywords} {$lang.hot_search} :  {foreach from...

PHP 文件上传$_FILES

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

发表评论

访客

看不清,换一张

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