The ultimate way: using PHP and Vue to develop innovative brain mapping functions
Introduction:
With the advent of the era of information explosion, people need to be more efficient Organize and process massive amounts of information. As a visual information organization method, mind mapping is widely used in fields such as mind mapping, project management, and knowledge organization. Using PHP and Vue to develop innovative brain mapping functions will further improve the efficiency of information organization and management. This article will introduce how to use PHP and Vue to implement a simple and powerful brain mapping function, and attach corresponding code examples.
First, we need to create a database table to store node information. You can use the following SQL statement to create a simple node table:
CREATE TABLE `nodes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `parent_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Then, we can use the PDO extension library provided by PHP to connect to the database and write the corresponding interface function for the front-end to call. The sample code is as follows:
<?php $dbHost = 'localhost'; $dbName = 'your_database_name'; $dbUser = 'your_username'; $dbPass = 'your_password'; $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8mb4", $dbUser, $dbPass); function getNodes($parentId = null) { global $pdo; $query = $pdo->prepare("SELECT * FROM nodes WHERE parent_id = ?"); $query->execute([$parentId]); return $query->fetchAll(PDO::FETCH_ASSOC); } function addNode($title, $parent_id) { global $pdo; $query = $pdo->prepare("INSERT INTO nodes (title, parent_id) VALUES (?, ?)"); $query->execute([$title, $parent_id]); return $pdo->lastInsertId(); }
First, we need to introduce Vue’s CDN library and create a Vue instance. Then, define a nodes array in the data of the Vue instance to store the node data obtained from the server. At the same time, we can write corresponding methods to handle the expansion and addition of nodes. The sample code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Brainmap</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <ul> <li v-for="node in nodes" @click="toggleNode(node)"> {{ node.title }} <ul v-if="node.expanded"> <li v-for="child in getChildren(node.id)" @click.stop="toggleNode(child)"> {{ child.title }} </li> </ul> </li> </ul> <input type="text" v-model="newNodeTitle"> <button @click="addNode">Add</button> </div> <script> new Vue({ el: '#app', data: { nodes: [], newNodeTitle: '' }, mounted() { this.loadNodes(); }, methods: { loadNodes() { // 调用服务器端接口获取节点数据 axios.get('/api/getNodes.php') .then(response => { this.nodes = response.data; }) .catch(error => { console.error(error); }); }, getChildren(parentId) { return this.nodes.filter(node => node.parent_id === parentId); }, toggleNode(node) { node.expanded = !node.expanded; }, addNode() { const newNode = { title: this.newNodeTitle, parent_id: null, expanded: false }; // 调用服务器端接口添加节点 axios.post('/api/addNode.php', newNode) .then(response => { newNode.id = response.data; this.nodes.push(newNode); this.newNodeTitle = ''; }) .catch(error => { console.error(error); }); } } }); </script> </body> </html>
The above is the detailed content of The ultimate way: using PHP and Vue to develop innovative mind mapping functions. For more information, please follow other related articles on the PHP Chinese website!