1.主页面代码:
主类->
$sql = "select * from xiangce_fenlei where id='".$row1['bigclass']."' order by id asc ";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) > 0) {// 输出小程序数组
while($row = mysql_fetch_array($result)){
?>
现为:=$row['title']?>
}} ?>
$sql = "select * from xiangce_fenlei where bigclass='0' order by id asc ";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) > 0) {// 输出小程序数组
while($row = mysql_fetch_array($result)){
?>
改正为:=$row['title']?>
}} ?>
子类->
$sql = "select * from xiangce_fenlei where id='".$row1['class']."' order by id asc ";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) > 0) {// 输出小程序数组
while($row = mysql_fetch_array($result)){
?>
现为:=$row['title']?>
}} ?>
* 选择《主类》后需等待1-2秒加载《子类》
function file_name(tt)
{
var win=window.open(tt,"window","location=no")
win.resizeTo(600,250)
win.moveTo(300,500)
}
$(document).ready(function(){
//不需要一开始就重新加载 getSelectVal();
$("#bigname").change(function(){
getSelectVal();
console.log("d1")
});
});
function getSelectVal(){
console.log("d2")
$.getJSON("api_class.php?bigname="+$("#bigname").val(),function(json){
var smallname = $("#smallname");
console.log('f'+$("#bigname").val());
console.log("d3")
$("option",smallname).remove(); //清空原有的选项
$.each(json,function(index,array){
var option = "更正为:"+array['title']+"";
smallname.append(option);
});
});
}
function newbigclass(){
//alert("重新加载中");
console.log("d4");
$.getJSON("api_bigclass.php",function(json){
var bigname = $("#bigname");
console.log("d5"+bigname);
$("option",bigname).remove(); //清空原有的选项
$.each(json,function(index,array){
var option = ""+array['title']+"";
bigname.append(option);
});
});
$.getJSON("api_class.php?zt=1",function(json){
var smallname = $("#smallname");
console.log("d6"+smallname);
$("option",smallname).remove(); //清空原有的选项
$.each(json,function(index,array){
var option = ""+array['title']+"";
console.log("d7"+option);
smallname.append(option);
});
});
}
2.后台一级分类获取页面
$q=mysql_query("select * from fenlei where bigclass = 0 and yh_id='".$_SESSION['yhid']."' order by id asc ");
while($row=mysql_fetch_array($q)){
$select[] = array("id"=>$row[id],"title"=>$row[title]);
}
echo json_encode($select);
3.后台 二级分类获取页面
$bigid = $_GET["bigname"];
if($bigid=="" and $_GET['zt']==1){
$sql1 ="select * from fenleiwhere yh_id='".$_SESSION['yhid']."' order by id asc limit 1 ";
$result1 = mysql_query($sql1,$conn); //查询
$num_rows1 =mysql_num_rows($result1);//记录总数
if ($num_rows1>0){
while($row1 = mysql_fetch_array($result1)){
$bigid=$row1['id'];
}}
}
if(isset($bigid)){
$q=mysql_query("select * from fenlei where bigclass = $bigid and yh_id='".$_SESSION['yhid']."' order by id asc");
while($row=mysql_fetch_array($q)){
$select[] = array("id"=>$row[id],"title"=>$row[title]);
}
echo json_encode($select);
}
本文标签:这篇文章木有标签
版权声明:若无特殊注明,本文皆为《
傲世零零 》原创,转载请保留文章出处。
本文链接:下拉菜单自动获取一二级菜单数据 php+mysql http://www.wangjunjiang.com/?post=143
正文到此结束