Home Backend Development PHP Tutorial Detailed explanation of the effect of secondary linkage menu implemented by php+mysql

Detailed explanation of the effect of secondary linkage menu implemented by php+mysql

Jul 29, 2016 am 08:49 AM
gt lt mysql

The example in this article describes the secondary linkage menu effect achieved by php+mysql. Share it with everyone for your reference, the details are as follows:

<!--php+mysql二级联动-->
<html>
<head>
<meta http-equiv="Content-Type" c />
<title>生成学院专业级联下拉菜单测试 </title>
</head>
<body>
<&#63; //
/***********************************************
** 功 能: php+mysql+javascript实现学院专业二级级联下拉框
** 数据库:数据库名( dms)、数据表( colleges、 majors)
** 表 colleges中字段: college_id( id编号)、 name(学院名)
** 表 majors中的字段: major_id( id编号)、 college_id(学院 ID)、 name(学院名)
** version 1.0
** 作 者: wu yaowen
***********************************************/
//****************** 连接选择数据库 ***************
$link = mysql_connect("localhost", "root", "123456")
  or die("Could not connect : " . mysql_error());
mysql_select_db("dms") or die("Could not select database");
//******************提取学院信息 ******************
$queryCol = "select * from colleges order by college_id ";
mysql_query("SET NAMES 'gb2312'");
$result1 = mysql_query($queryCol) or die("Query failed : " . mysql_error());
$colleges = array();
while( $row1 = mysql_fetch_array($result1) )
{
  $colleges[] = $row1;
}
//print_r ($forum_data);
mysql_free_result($result1);
//**************获取专业信息 **************  
$queryMaj = "select * from majors order by college_id desc";
mysql_query("SET NAMES 'gb2312'");
if( !($result2 = mysql_query($queryMaj)) )
{
  die('Could not query t_city list');
}
$majors = array();
while( $row2 = mysql_fetch_array($result2) )
{
  $majors[] = $row2;
}
mysql_free_result($result2);
&#63;>
<!--************ JavaScript处理 college-onChange *************-->
<script language = "JavaScript">
 var majorCount; // 存储专业记录条数
 // form_majors[] 储存专业 major数据,如 {(1,1,电子商务 ),(4,1,计算机科学 ),(3,2,古典文学 )}
 form_majors = new Array();
 <&#63;php
   $num2 = count($majors); // $num2 获取专业表中记录的个数
 &#63;>
   majorCount = <&#63;php echo $num2;&#63;>;
 <&#63;
   for($j=0;$j<$num2;$j++) // 从 0开始取出上面 majors[]中存储的专业数据填充数组
 {
 &#63;>
   form_majors[<&#63;echo $j;&#63;>] = new Array("<&#63;echo $majors[$j]['major_id'];&#63;>","<&#63;echo $majors[$j]['college_id'];&#63;>","<&#63;echo $majors[$j]['name'];&#63;>");
 <&#63;php
 }
 &#63;>
 function changeCollege(college_id)
 {
   document.stu_add_form.major.length = 0;
   var id=id;
   var j;
   document.stu_add_form.major.options[0] = new Option('==选择专业 ==',''); // label的 value为空 ' '
   for (j=0;j < majorCount; j++) // 从 0开始判断
   {
    if (form_majors[j][1] == college_id) // if college_id等于选择的学院的 id
    {
       document.stu_add_form.major.options[document.stu_add_form.major.length] = new Option(form_majors[j][2], form_majors[j][0]);
    }
   }
 }
</script>
<!--********************页面表单 *************************-->
<form name="stu_add_form" method="post">
选择: <select name="college"  size="1">
<option selected>==请选择学院 ==</option>
<&#63;php
$num = count($colleges);
for($i=0;$i<$num;$i++)
{
&#63;>
<option value="<&#63;echo $colleges[$i]['college_id'];&#63;>"><&#63;echo $colleges[$i]['name'];&#63;></option>
<&#63;
}
&#63;>
</select>
<select name="major">
<option selected value="">==选择专业 ==</option>
</select>
</form>
</body>
</html>

Copy after login

sql statement:

--
-- 表的结构 `colleges`
--
CREATE TABLE IF NOT EXISTS `colleges` (
 `college_id` int(8) NOT NULL auto_increment COMMENT '学院编号自动增加',
 `name` varchar(40) NOT NULL COMMENT '学院名称',
 PRIMARY KEY (`college_id`),
 UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=7 ;
--
-- 导出表中的数据 `colleges`
--
INSERT INTO `colleges` (`college_id`, `name`) VALUES
(4, '化学与化工学院'),
(1, '计算机与信息科学学院'),
(6, '美术学院'),
(2, '文学院'),
(5, '音乐学院'),
(3, '政治与公共管理学院');
--
-- 表的结构 `majors`
--
CREATE TABLE IF NOT EXISTS `majors` (
 `major_id` int(8) NOT NULL auto_increment COMMENT '专业号,自动增加',
 `name` varchar(40) NOT NULL COMMENT '专业名',
 `college_id` int(8) default NULL COMMENT '所在学院',
 `counsellor_id` int(10) default NULL COMMENT '辅导员',
 PRIMARY KEY (`major_id`),
 UNIQUE KEY `college_id` (`college_id`,`counsellor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=8 ;
--
-- 导出表中的数据 `majors`
--
INSERT INTO `majors` (`major_id`, `name`, `college_id`, `counsellor_id`) VALUES
(1, '电子商务', 1, 1),
(2, '音乐视唱', 5, 1),
(3, '古典文学', 2, 1),
(4, '计算机科学', 1, NULL),
(5, '自动化', 1, NULL),
(6, '现代文学', 2, NULL),
(7, '新闻写作', 2, NULL);

Copy after login

Readers who are interested in more PHP related content can check out the special topics of this site: "Summary of PHP database operation skills based on pdo", " PHP+MongoDB database operation skills collection", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

The above has introduced a detailed explanation of the effect of the secondary linkage menu implemented by php+mysql, including Mysql content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles