繁体   English   中英

如何将 json 结果插入到 WordPress 表中?

[英]How can I insert json results into a WordPress table?

我在 WordPress 中有一个生成 json 的自定义页面,我想插入到 WordPress 数据库中的自定义表中。 我该怎么做?

 {"horarios":[{"dia":"2017-10-25","hora_inicio":"17:00","hora_termino":"17:00"},{"dia":"2017-10-26","hora_inicio":"13:30","hora_termino":"16:00"}]}

谢谢。

将表格数据插入 Wordpress 自定义表格。

第一步:将mysql表转换为json文件https://www.csvjson.com/sql2json

第 2 步:导出表并粘贴到https://www.csvjson.com/sql2json

/**
 * Continents 
 */
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `continents`
-- ----------------------------
DROP TABLE IF EXISTS `continents`;
CREATE TABLE `continents` (
  `code` char(2) NOT NULL COMMENT 'Continent code',
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

-- ----------------------------
-- Records of continents
-- ----------------------------
INSERT INTO `continents` VALUES ('AF', 'Africa');
INSERT INTO `continents` VALUES ('AN', 'Antarctica');
INSERT INTO `continents` VALUES ('AS', 'Asia');
INSERT INTO `continents` VALUES ('EU', 'Europe');
INSERT INTO `continents` VALUES ('NA', 'North America');
INSERT INTO `continents` VALUES ('OC', 'Oceania');
INSERT INTO `continents` VALUES ('SA', 'South America');
INSERT INTO `continents` VALUES ('??', NULL);

步骤3.创建目录数据

步骤 4. 将 json 文件放入数据目录。

步骤5:将波纹管代码放入插件主插件文件或任何文件中。

register_activation_hook(__FILE__, 'plugin_name_install_data');

function plugin_name_install_data()
{
    global $wpdb;

    $table_name = $wpdb->prefix . 'continents'; 

    $contactjsondata = file_get_contents(plugin_dir_path( __FILE__ ) . 'data/continents.json');
    $data = json_decode($contactjsondata, true);

    foreach ($data['continents'] as $single_data) {

        $item=array(
            'code'=> $single_data['code'],
            'name' => $single_data['name']
        );

        $result = $wpdb->insert($table_name, $item);

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM