PHP无限分类,js无限联动

JavaScript012

PHP无限分类,js无限联动,第1张

php先是成数组,然后传递给js

举个例子

php的

<?

$array = array(

array(1,'类别1',0),

array(2,'类别1',1),

array(3,'类别1',2),

array(4,'类别1',1),

//分别是 自身id(唯一) 名称 父id( 0的时候是跟)

)

?>

<script>

var array= eval(<? echo json_encode($array) ?>)

//s_id 是自身id 调用的时候传入0 从跟开始

function tree(s_id)

{

这里遍历array数组,查找父id == s_id的匹配

array(1,'类别1',0), 这条符合 0

找到匹配后查找当前id 1 有没有儿子, 有儿子的就是能展开的,继续调用 tree(id) 不断的去递归

没儿子的不展开

}

<script>

给ul加个id="source",方便操作。

$(function(){

var $s = $("#source").find("li").each(function(){

var $this = $(this),id = $this.attr("title"),

parent_id = $this.children()[0].getAttribute("title")

// 判断是否子节点

if(id != parent_id){

var $p = $("li[title="+parent_id+"]",$s)

// 判断父节点是否有嵌套ul

if($p.children("ul").length >0){

$($p.children("ul")).append($this)

}else{

var $ul = $("<ul>").append($this)

$p.append($ul)

}

}

})

})

<?php mysql_connect("localhost","root","123") mysql_select_db("ceshi") ?><script language = "JavaScript"> var onecount onecount=0 subcat = new Array() <?php$sql = "select * from news_sclass" $result = mysql_query($sql) $count = 0 while($res = mysql_fetch_row($result))echo "onecount=$count" ?> //联动函数function changelocation(locationid)}}</script> <form method="post" name="myform" action="1.php"> <select name="type" onChange="changelocation(document.myform.type.options[document.myform.type.selectedIndex].value)" size="1"> <option selected value="">main</option> <?php$sql = "select * from news_bclass" $result = mysql_query($sql) while($res = mysql_fetch_row($result)) ?> </select> <select name="ctype"> <option selected value="">sub</option> </select> <input type="submit" name="Submit" value="search"> </form> 数据库ceshi,news_sclass 有三个字段小类表名ID 小类名 大类名news_bclass大类表 有两个字段ID 大类名 希望我的答案令你满意