• 递归读取输出无限分类目录


    php递归值自动读取无限分类目录

    对于数据库最简单的一张表实现,建立category表,字段分别为ID,name,f_id.

    ID为自增主键,name为类名,f_id为父级目录,默认为根目录0.

    <?php
    header ( "content-type:text/html; charset=utf-8" );
    mysql_connect ( "localhost", "admin", "1123581321" );
    mysql_query ( "set names utf8" );
    
    ?>
    <?php
    
    function getmenu($f_id = 0) {
    	$menu_arr = array ();
    	$sql = "SELECT * FROM test.`category` where f_id=" . $f_id;
    	$result = mysql_query ( $sql );
    	if (! $result) {
    		return false;
    	}
    	$i = 1;
    	$row = "";
    	while ( $row = mysql_fetch_assoc ( $result ) ) {
    		$menu_arr [$i] = $row;
    		$menu_arr[$i]["next"]=getmenu($row['ID']);
    		$i ++;
    	}
    	return $menu_arr;
    }
    $menus = getmenu ();
    echo "<pre>";
    print_r ( $menus );
    echo "</pre>";
    ?>
    

    输出结构为

    Array
    (
        [1] => Array
            (
                [ID] => 1
                [name] => 前端
                [f_id] => 0
                [next] => Array
                    (
                        [1] => Array
                            (
                                [ID] => 5
                                [name] => ajax
                                [f_id] => 1
                                [next] => Array
                                    (
                                        [1] => Array
                                            (
                                                [ID] => 11
                                                [name] => EXTJS
                                                [f_id] => 5
                                                [next] => Array
                                                    (
                                                    )
    
                                            )
    
                                    )
    
                            )
    
                        [2] => Array
                            (
                                [ID] => 6
                                [name] => javascript
                                [f_id] => 1
                                [next] => Array
                                    (
                                        [1] => Array
                                            (
                                                [ID] => 10
                                                [name] => JQUERY
                                                [f_id] => 6
                                                [next] => Array
                                                    (
                                                    )
    
                                            )
    
                                    )
    
                            )
    
                        [3] => Array
                            (
                                [ID] => 7
                                [name] => html5
                                [f_id] => 1
                                [next] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
        [2] => Array
            (
                [ID] => 2
                [name] => php
                [f_id] => 0
                [next] => Array
                    (
                        [1] => Array
                            (
                                [ID] => 8
                                [name] => wordpress
                                [f_id] => 2
                                [next] => Array
                                    (
                                    )
    
                            )
    
                        [2] => Array
                            (
                                [ID] => 9
                                [name] => discuz
                                [f_id] => 2
                                [next] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
        [3] => Array
            (
                [ID] => 4
                [name] => net
                [f_id] => 0
                [next] => Array
                    (
                    )
    
            )
    
    )
    

     最后递归在前台输出

    <?php 
    function pri($menus){
    	foreach ($menus as $va){
        echo "<div>".$va["name"];
        if($va["next"]){
        	pri($va["next"]);
        	
        }
        echo "</div>";
    	}
    	
    }
    pri($menus);
    ?>
    

     结构为

    <body>
    <div>
    	前端
    	<div>
    		ajax
    		<div>
    			EXTJS
    		</div>
    	</div>
    	<div>
    		javascript
    		<div>
    			JQUERY
    		</div>
    	</div>
    	<div>
    		html5
    	</div>
    </div>
    <div>
    	php
    	<div>
    		wordpress
    	</div>
    	<div>
    		discuz
    	</div>
    </div>
    <div>
    	net
    </div>
    </body>
    

     为了实现js的菜单的联动效果,结构也可以写成

    <?php function pri($menu){?>
    <div><?php foreach ($menu as $va){?>
    <div <?php if($va["next"]){?> class="title" <?php }?>><?php echo $va["name"]?></div>
    <?php if($va["next"]){pri($va["next"]);}?>
    <?php }?></div>
    <?php }?>
    
    <?php pri($menus)?>
    

     html为

    <div>
    	<div class="title">
    		前端
    	</div>
    	<div>
    		<div class="title">
    			ajax
    		</div>
    		<div>
    			<div>
    				EXTJS
    			</div>
    		</div>
    		<div class="title">
    			javascript
    		</div>
    		<div>
    			<div>
    				JQUERY
    			</div>
    		</div>
    		<div>
    			html5
    		</div>
    	</div>
    	<div class="title">
    		php
    	</div>
    	<div>
    		<div>
    			wordpress
    		</div>
    		<div>
    			discuz
    		</div>
    	</div>
    	<div>
    		net
    	</div>
    </div>
    
  • 相关阅读:
    2017/3/27 morning
    2017/3/24 afternoon
    2017/3/24 morning
    2017/3/21 afternoon
    2017/3/21 morning
    2017/3/20 afternoon
    2017/3/20 morning
    2017/3/16 afternoon
    2017/3/16 morning
    2017/3/15afternoon
  • 原文地址:https://www.cnblogs.com/breakdown/p/2433419.html
Copyright © 2020-2023  润新知