The method for phpcms to achieve second-level linkage is: 1. Create two tables in the database for first-level classification and second-level classification; 2. Create a PHP page and set two drop-down boxes on the page , the first drop-down box is filled with the contents of the first-level classification list, and the second drop-down box should retrieve appropriate data from the second-level classification table according to the selected first-level classification and fill it accordingly; 3. Use Ajax to call after selection Send a request to the server to fill the second drop-down box; 4. Write a PHP script on the server side to respond to the request sent by the client.
The operating system of this tutorial: Windows 10 system, DedeCMS version 5.7.109, Dell G3 computer.
You can achieve second-level linkage through the following steps:
Create two tables in the database, one for first-level classification (such as major categories), and the other for in secondary classifications (such as subcategories). Among them, the second-level classification table should contain a field that references the ID of the first-level classification table.
Create a PHP page and set two drop-down boxes on the page. The first drop-down box is filled with the content of the first-level classification list. When the user selects a primary classification, the second drop-down box should retrieve the appropriate data from the secondary classification table based on the selected primary classification and populate accordingly.
When the user visits the page for the first time, only the content in the first drop-down box is displayed, and no content in the second drop-down box is displayed. The user can select an item in the first dropdown box and upon selection use an Ajax call to send a request to the server to populate the second dropdown box.
Write a PHP script on the server side to respond to requests made by the client. This script will query all second-level classifications corresponding to the selected first-level classification ID and return the results to the client.
The following is a simple PHP code example, which involves connecting to the MySQL database and querying the corresponding subcategory:
```php <?php // 连接数据库 $hostname = 'localhost'; $username = 'root'; $password = ''; $database = 'test_db'; $con=mysqli_connect($hostname, $username, $password,$database) or die('连接失败'); // 读取一级分类列表项的值 $category = $_GET['category']; // 根据所选分类查询子分类列表 $query="SELECT * FROM sub_categories WHERE category_id='$category'"; $result=mysqli_query($con,$query); ?> ```
In addition, JavaScript and AJAX are also required to implement Asynchronous loading and updating of data is displayed on the page. The end result will be a dynamic drop-down box that interacts with the backend.
The above is the detailed content of How to achieve secondary linkage in phpcms. For more information, please follow other related articles on the PHP Chinese website!