php js实现数据库四级分类栏目(可以做到7级)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haibo0668/article/details/76128510

代码:

<?php
include("config.php");
?>
<head>
<meta charset="utf-8">
<title>增加记录</title>
<link href="manage.css" rel="stylesheet" type="text/css" />

<br />
<!--
id 表ID(唯一)
title 各类标题
flid 类别的ID (大类为1 中类为2 小类为3)
pid 上类的ID(大类就跟大类,提交中类的时候这地方写大类的ID,提交小类的时候写中类的ID) )
-->
<script language="JavaScript">
<!--
var subcat = new Array();
<?php
$i=0;


//$sql="select * from s_type where flid=2";
//
// $conn=mysql_query($sql);
// while($arr=mysql_fetch_array($conn)){
//$query=mysql_query($sql,$link);
//while($arr=mysql_fetch_array($query))
//{
$sql_saleinfo = "SELECT * FROM data_type where flid=2 order by uid desc";
$rs_saleinfo = mysqli_query($conn, $sql_saleinfo);
while($arr = mysqli_fetch_assoc($rs_saleinfo)) 
{ 

echo "subcat[".$i++."] = new Array('".$arr["pid"]."','".$arr["title"]."','".$arr["id"]."');\n";
}
?>
var subcat2 = new Array();
<?php
$i=0;
$sql_saleinfo = "SELECT * FROM data_type where flid=3 order by uid desc";
$rs_saleinfo = mysqli_query($conn, $sql_saleinfo);
while($arr = mysqli_fetch_assoc($rs_saleinfo)) 
{ 

echo "subcat2[".$i++."] = new Array('".$arr["pid"]."','".$arr["title"]."','".$arr["id"]."');\n";
}
?>
var subcat3 = new Array();
<?php
$i=0;
$sql_saleinfo = "SELECT * FROM data_type where flid=4 order by uid desc";
$rs_saleinfo = mysqli_query($conn, $sql_saleinfo);
while($arr = mysqli_fetch_assoc($rs_saleinfo)) 
{ 

echo "subcat3[".$i++."] = new Array('".$arr["pid"]."','".$arr["title"]."','".$arr["id"]."');\n";
}
?>
//四级联动(一级项目、二级项目、三级项目、班型)
function changeselect1(locationid)
{
document.Form1.s2.length = 0;
document.Form1.s2.options[0] = new Option('==请选择==','');
for (i=0; i<subcat.length; i++)
{
if (subcat[i][0] == locationid)
{document.Form1.s2.options[document.Form1.s2.length] = new Option(subcat[i][1], subcat[i][2]);}
}
}
function changeselect2(locationid)
{
document.Form1.s3.length = 0;
document.Form1.s3.options[0] = new Option('==请选择==','');
for (i=0; i<subcat2.length; i++)
{
if (subcat2[i][0] == locationid)
{document.Form1.s3.options[document.Form1.s3.length] = new Option(subcat2[i][1], subcat2[i][2]);}
}
}
function changeselect3(locationid)
{
document.Form1.s4.length = 0;
document.Form1.s4.options[0] = new Option('==请选择==','');
for (i=0; i<subcat3.length; i++)
{
if (subcat3[i][0] == locationid)
{document.Form1.s4.options[document.Form1.s4.length] = new Option(subcat3[i][1], subcat3[i][2]);}
}
}
//三级联动(班级名称、班次、班号)
function changeselect5(locationid)
{
document.Form1.s6.length = 0;
document.Form1.s6.options[0] = new Option('==请选择==','');
for (i=0; i<subcat.length; i++)
{
if (subcat[i][0] == locationid)
{document.Form1.s6.options[document.Form1.s6.length] = new Option(subcat[i][1], subcat[i][2]);}
}
}
function changeselect6(locationid)
{
document.Form1.s7.length = 0;
document.Form1.s7.options[0] = new Option('==请选择==','');
for (i=0; i<subcat2.length; i++)
{
if (subcat2[i][0] == locationid)
{document.Form1.s7.options[document.Form1.s7.length] = new Option(subcat2[i][1], subcat2[i][2]);}
}
}
//-->
</script>
<form action="" method="post" name="Form1" id="Form1" onSubmit="return FormCheck();">
  <br>
<br>
<br>
<table width="98%" border="1" align="center" cellpadding="2" cellspacing="0"  bordercolor="#333333"  style="border-collapse: collapse; border-style: dotted; border-width: 0px">
  <tr>
    <td width="8%" align="center" bgcolor="#1EA2DB" class=td onClick="javascript:left_menu('left_4');">课程信息</td>
    <td width="92%" bgcolor="#F7EEEF" onClick="javascript:left_menu('left_4');"> </td>
  </tr>
  <tr id=left_4 style="">
    <td colspan="2"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="text">
      <tr>
        <td width="10%" align="right" bgcolor="#E7E7E7" class="linef1" style="padding-right:5px">一级项目</td>
        <td width="23%" align="left" bgcolor="#E7E7E7" class="linef1"><select name="s1" onChange="changeselect1(this.value)">
                <option value="">==请选择==</option>
              <?php if ($es1!=""){?>
              <option value="<?php echo $es1 ?>" selected><?php echo $eproject?></option>
              <?php }?>
              <?php
$sql_saleinfo = "SELECT * FROM data_type where flid=1 order by uid desc";
$rs_saleinfo = mysqli_query($conn, $sql_saleinfo);
while($arr = mysqli_fetch_assoc($rs_saleinfo)) 
{ 
              echo "<option value=".$arr["id"].">".$arr["title"]."</option>\n";
              }
              ?>
              </select>
          <span class="red">*</span></td>
        <td width="13%" align="right" bgcolor="#E7E7E7" class="linef1" style="padding-right:5px">二级项目</td>
        <td width="18%" align="left" bgcolor="#E7E7E7" class="linef1">
            <select name="s2" onChange="changeselect2(this.value)">
            <option value="">==请选择==</option>
              <?php if ($es2!=""){?>
              <option value="<?php echo $es2 ?>" selected><?php echo $ejibie?></option>
              <?php }?>
            </select>
            <span class="red">*</span></td>
        <td width="13%" align="right" bgcolor="#E7E7E7" class="linef1" style="padding-right:5px">三级项目</td>
        <td width="23%" align="left" bgcolor="#E7E7E7" class="linef1r">
            <select name="s3" onChange="changeselect3(this.value)">
            <option value="">==请选择==</option>
              <?php if ($es3!=""){?>
              <option value="<?php echo $es3 ?>" selected><?php echo $esanjixiangmu?></option>
              <?php }?>
            </select>
            <span class="red">*</span></td>
      </tr>
      <tr>
        <td align="right" bgcolor="#E7E7E7" class="linef1" style="padding-right:5px">班型</td>
        <td align="left" bgcolor="#E7E7E7" class="linef1">
            <select name="s4">
            <option value="">==请选择==</option>
              <?php if ($es4!=""){?>
              <option value="<?php echo $es4 ?>" selected><?php echo $emodel?></option>
              <?php }?>
            </select>
            <span class="red">* </span></td>
        <td align="right" bgcolor="#E7E7E7" class="linef1" style="padding-right:5px"> </td>
        <td align="left" bgcolor="#E7E7E7" class="linef1"> </td>
        <td align="right" bgcolor="#E7E7E7" class="linef1" style="padding-right:5px"> </td>
        <td align="left" bgcolor="#E7E7E7" class="linef1r"> </td>
      </tr>
 
      </table></td>
  </tr>
</table>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</form>

</body>
</html>


注意:<form action="" method="post" name="Form1" id="Form1" onSubmit="return FormCheck();">

要与JS代码名称对应一标



config.php

代码

<?php
session_start();
header("Content-type: text/html;charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "xxxxxx";


// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
    /* Close the connection 关闭连接*/     
   // mysqli_close($conn);     
date_default_timezone_set('prc');//时区问题,php环境默认时差与北京时间相差8小时,我们要想获取正确的时间就必须设置在PHP文件开始处 加上date_default_timezone_set('prc');
 

ini_set("error_reporting","E_ALL & ~E_NOTICE");//屏蔽错误信息




?>


数据库设计


展开阅读全文

没有更多推荐了,返回首页