|
楼主 |
发表于 2023-5-15 00:43:17
|
显示全部楼层
以下是一个简单的美容院收支明细表插件代码,使用 WordPress 做为网站平台。
1. 首先,在 WordPress 后台管理页面中创建一个新的 Plugin(插件),命名为 Beauty Salon Income Details 或者其他你喜欢的名称。
2. 在该 Plugin 目录下创建一个名为 `bsid.php` 的 PHP 文件,并将以下代码复制到文件中:
- <?php
- /**
- * Plugin Name: Beauty Salon Income Details
- * Description: A plugin that allows you to track your beauty salon's income and expenses.
- * Version: 1.0
- * Author: Your Name
- * License: GPL2
- */
- // create the shortcode
- function bsid_shortcode() {
- // check if user is logged in, only allow logged in users to view the details
- if ( is_user_logged_in() ) {
- // get the current year
- $year = date('Y');
- // query the database for income details by month and year
- global $wpdb;
- $results = $wpdb->get_results( "
- SELECT MONTH(date) AS month, SUM(income) AS income, SUM(expense) AS expense
- FROM incomes
- WHERE YEAR(date) = $year
- GROUP BY MONTH(date)
- " );
- // output the results in a table format
- $output = '<table>';
- $output .= '<thead><tr><th>Month</th><th>Income</th><th>Expense</th></tr></thead>';
- $output .= '<tbody>';
- foreach ( $results as $result ) {
- $month = date("F", mktime(0, 0, 0, $result->month, 10));
- $income = $result->income ? number_format($result->income, 2) : '--';
- $expense = $result->expense ? number_format($result->expense, 2) : '--';
- $output .= "<tr><td>$month</td><td>$income</td><td>$expense</td></tr>";
- }
- $output .= '</tbody>';
- $output .= '</table>';
- return $output;
- } else {
- // if user is not logged in, show a message to prompt them to log in
- return '<p>Please log in to view beauty salon income details.</p>';
- }
- }
- add_shortcode( 'bsid', 'bsid_shortcode' );
复制代码
3. 在该插件目录下创建一个SQL文件,命名为 `install.sql`,将以下代码复制到文件中:
- CREATE TABLE IF NOT EXISTS `incomes` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `date` date NOT NULL,
- `income` decimal(10,2) DEFAULT NULL,
- `expense` decimal(10,2) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- INSERT INTO `incomes` (`id`, `date`, `income`, `expense`) VALUES (1, '2021-01-01', '1500.00', '500.00');
- INSERT INTO `incomes` (`id`, `date`, `income`, `expense`) VALUES (2, '2021-01-05', '2000.00', '600.00');
- INSERT INTO `incomes` (`id`, `date`, `income`, `expense`) VALUES (3, '2021-02-01', '1800.00', '550.00');
- INSERT INTO `incomes` (`id`, `date`, `income`, `expense`) VALUES (4, '2021-02-05', '2200.00', '750.00');
- INSERT INTO `incomes` (`id`, `date`, `income`, `expense`) VALUES (5, '2021-03-01', '1900.00', '600.00');
- INSERT INTO `incomes` (`id`, `date`, `income`, `expense`) VALUES (6, '2021-03-05', '2300.00', '800.00');
复制代码
该 SQL 文件包含一个名为 `incomes` 的表格,用于存储各月份的收入和支出细节。
4. 接着,在 WordPress 后台管理页面中安装并激活该插件,这将自动运行 install.sql 文件,并在数据库中创建表格和添加数据。
5. 最后,在任意页面或文章中输入 `[bsid]` 短代码,该短代码将自动渲染出美容院收支明细表。用户需要登陆到你的网站才能看到这张表格。
注意:该插件只是一个简单的例子,你可以根据自己的需求和业务逻辑来修改代码实现更多的功能。 |
|