Baidu Wenxin Yiyan is a popular sentence-sharing platform that collects various types of beautiful sentences. If you want to implement a similar sentence categorization display feature on your own website, this article will guide you through the development process using PHP.
First, we need to prepare a database with two tables to store sentence categories and their content.
We can create a table called categories
Next, we need to create a table for storing sentences, called sentences, with three fields:
Here’s an example SQL to create the sentences table:
CREATE TABLE `sentences` ( `id` int(11) NOT NULL AUTO_INCREMENT, `content` varchar(255) NOT NULL, `category_id` int(11) NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `fk_sentences_categories` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Next, we can insert some test data into the categories table. For example:
INSERT INTO `categories` (`name`) VALUES ('Love'); INSERT INTO `categories` (`name`) VALUES ('Friendship'); INSERT INTO `categories` (`name`) VALUES ('Motivation');
Then, insert some sentence data into the sentences table:
INSERT INTO `sentences` (`content`, `category_id`) VALUES ('Love sentence 1', 1); INSERT INTO `sentences` (`content`, `category_id`) VALUES ('Love sentence 2', 1); INSERT INTO `sentences` (`content`, `category_id`) VALUES ('Friendship sentence 1', 2); INSERT INTO `sentences` (`content`, `category_id`) VALUES ('Friendship sentence 2', 2); INSERT INTO `sentences` (`content`, `category_id`) VALUES ('Motivation sentence 1', 3); INSERT INTO `sentences` (`content`, `category_id`) VALUES ('Motivation sentence 2', 3);
Once the database design and data insertion are complete, the next step is to write PHP code to display the sentences by category.
First, we need to connect to the MySQL database:
<?php $host = 'localhost'; $db = 'your_database_name'; $user = 'your_username'; $password = 'your_password'; $dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4"; try { $pdo = new PDO($dsn, $user, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); exit; } ?>
Next, we can query the database to fetch all categories and the sentences under each category:
<?php $sql = 'SELECT * FROM `categories`'; $categories = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); foreach ($categories as $category) { echo '<h2>' . $category['name'] . '</h2>'; $sql = 'SELECT * FROM `sentences` WHERE `category_id` = ' . $category['id']; $sentences = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); echo '<ul>'; foreach ($sentences as $sentence) { echo '<li>' . $sentence['content'] . '</li>'; } echo '</ul>'; } ?>
With the code examples above, we can implement a simple sentence categorization display feature, similar to the one used by Baidu Wenxin Yiyan. You can further expand and optimize it according to your needs.
We hope this article helps you implement this feature and provides you with some insights for your PHP development!