PHP code example of ionepub / region

1. Go to this page and download the library: Download ionepub/region library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ionepub / region example snippets




use Ionepub\Region;
try {
	// get a medoo instance
	$db = new medoo([
	    'database_type' => 'mysql',
	    'database_name' => 'test',
	    'server' => 'localhost',
	    'username' => 'root',
	    'password' => 'root',
	    'charset' => 'utf8'
	]);
	$table_name = 'region';
	// get a region instance
	// 第三个参数为true,表示首次安装,将在数据库中创建名为 $table_name 的表,并初始化地区数据
	$region = Region::init($db, $table_name, true);
	
	// 获取所有省份
	$province = $region->get();
    
	// 获取北京市信息
	$city_of_beijing = $region->get('110000000000');
    
	// 获取北京市下属区域信息
	$child_of_beijing = $region->child('110000000000');
    
	// 获取东华门街道办事处所在位置:北京市/市辖区/东城区/东华门街道办事处
	$position_of_region = $region->position('110101001000');
    
	// 查询名称中包含北京的地区信息
	$search_of_region = $region->search('北京');
    
	// 在省份中查询名称包含北京的地区信息
	$search_of_province = $region->search('北京', Region::LEVEL_PROVINCE);
    
	// 在所有地区中查询名称为“北京市”的地区信息(非模糊查询)
	$search3 = $region->search('北京市', Region::LEVEL_DEFAULT, Region::STRICT);
    
} catch (\Exception $e) {
	echo 'Error catched: ' . $e->getMessage();
}

$region = Region::init($db, $region_name, true);

$region->get();

$region->get(1);
$region->get('110000000000');
$region->get('110000000000', Region::LEVEL_PROVINCE); // 明确获取编码为110000000000的省份地区信息

$region->get(['110000000000', 355]); // 同时获取两个地区的信息
$region->get([], Region::LEVEL_PROVINCE); // 获取所有省份信息

$region->child(3);
$region->child('110000000000');
 
$region->child(3, Region::LEVEL_DEFAULT);

$region->position('110101001000');

$region->search('北京');

// 查询名称中包含北京的地区信息
$region->search('北京');
// 在省份中查询名称包含北京的地区信息
$region->search('北京', Region::LEVEL_PROVINCE);
// 在所有地区中查询名称为“北京市”的地区信息(非模糊查询)
$region->search('北京市', Region::LEVEL_DEFAULT, Region::STRICT);