Example of three level linkage menu function implemented by PHP+mysql
- 2021-11-24 01:05:16
- OfStack
In this paper, an example is given to describe the three-level linkage menu function realized by PHP+mysql. Share it for your reference, as follows:
Database mysql
-- Database : `student`
--
-- --------------------------------------------------------
--
-- Structure of the table `student`
--
CREATE TABLE `student` (
`id` int(50) NOT NULL auto_increment,
`name` varchar(50) collate utf8_unicode_ci NOT NULL,
`dept` varchar(50) collate utf8_unicode_ci NOT NULL,
`class` varchar(50) collate utf8_unicode_ci NOT NULL,
`sex` varchar(50) collate utf8_unicode_ci NOT NULL,
`dept_id` int(50) NOT NULL,
`class_id` int(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
--
-- Export data from a table `student`
--
INSERT INTO `student` (`id`, `name`, `dept`, `class`, `sex`, `dept_id`, `class_id`) VALUES
(1, ' Computer name ', ' Department of Computer Engineering ', ' Meter 061', ' Male ', 1, 11),
(2, ' Teaching plan name ', ' Department of Computer Engineering ', ' Teaching skills 061', ' Male ', 1, 14),
(3, ' Management name ', ' Department of Management ', ' Management 061', ' Female ', 2, 21),
(4, ' Machine name ', ' Department of Mechanical Engineering ', ' Automation 061', ' Male ', 3, 31);
index. php code:
<?php
$link=mysql_connect("localhost","root","") or die(" Database server connection error ".mysql_error());
mysql_select_db("student",$link) or die(" Database access error ".mysql_error());
mysql_query("set character set gb2312");
mysql_query("set names gb2312");
?>
<html>
<head>
<title> Drop-down frame linkage </title>
</head>
<body>
<script language="JavaScript">
//2 Level menu array
var subcat = new Array();
<?php
$i=0;
$sql="select * from student";
$query=mysql_query($sql,$link);
while($arr=mysql_fetch_array($query))
{
echo "subcat[".$i++."] = new Array('".$arr["dept_id"]."','".$arr["class"]."','".$arr["class_id"]."');\n";
}
?>
//3 Level menu array
var subcat2 = new Array();
<?php
$i=0;
$sql="select * from student";
$query=mysql_query($sql,$link);
while($arr=mysql_fetch_array($query))
{
echo "subcat2[".$i++."] = new Array('".$arr["class_id"]."','".$arr["class"]."','".$arr["name"]."');\n";
}
?>
function changeselect1(locationid)
{
document.form1.s2.length = 0;
document.form1.s2.options[0] = new Option('22 Please select 22','');
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('33 Please select 33','');
for (i=0; i<subcat2.length; i++)
{
if (subcat2[i][0] == locationid)
{
document.form1.s3.options[document.form1.s3.length] = new Option(subcat2[i][2], subcat2[i][0]);
}
}
}
</script>3 Level linkage demonstration: <BR>
<form name="form1">
<select name="s1" onChange="changeselect1(this.value)">
<option>-- Please select --</option>
<option value=1> Department of Computer Engineering </option>
<option value=2> Department of Management </option>
<option value=3> Department of Mechanical Engineering </option>
</select>
<select name="s2" onChange="changeselect2(this.value)">
<option>-- Please select --</option>
</select>
<select name="s3">
<option>-- Please select --</option>
</select>
</form>
</body>
</html>
For more readers interested in PHP related contents, please check the topics on this site: "Introduction to php+mysql Database Operation", "Summary of php+mysqli Database Programming Skills", "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.