PHP code example of jessebyarugaba / ug-address
1. Go to this page and download the library: Download jessebyarugaba/ug-address 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/ */
jessebyarugaba / ug-address example snippets
gAddress\UgAddress;
$addr = new UgAddress();
$districts = $addr->getDistricts();
print_r($districts);
Array
(
[0] => Array
(
[id] => 98
[name] => ABIM
)
[1] => Array
(
[id] => 1
[name] => ADJUMANI
)
)
gAddress\UgAddress;
$addr = new UgAddress();
# -----------------------------------
# Get districts
# -----------------------------------
$districts = $addr->getDistricts();
echo "DISTRICTS:\n\n";
foreach (array_slice($districts, 0, 5) as $district) {
echo $district['id']
. ' - '
. $district['name']
. PHP_EOL;
}
# -----------------------------------
# Select district
# -----------------------------------
$addr->selectDistrict('32'); // Kampala
$counties = $addr->getCounties();
echo "\nCOUNTIES:\n\n";
foreach ($counties as $county) {
echo $county['id']
. ' - '
. $county['name']
. PHP_EOL;
}
# -----------------------------------
# Select county
# -----------------------------------
$addr->selectCounty($counties[0]['id']);
$subcounties = $addr->getSubcounties();
echo "\nSUBCOUNTIES:\n\n";
foreach (array_slice($subcounties, 0, 5) as $subcounty) {
echo $subcounty['id']
. ' - '
. $subcounty['name']
. PHP_EOL;
}
# -----------------------------------
# Select subcounty
# -----------------------------------
$addr->selectSubcounty($subcounties[0]['id']);
$parishes = $addr->getParishes();
echo "\nPARISHES:\n\n";
foreach (array_slice($parishes, 0, 5) as $parish) {
echo $parish['id']
. ' - '
. $parish['name']
. PHP_EOL;
}
# -----------------------------------
# Select parish
# -----------------------------------
$addr->selectParish($parishes[0]['id']);
$villages = $addr->getVillages();
echo "\nVILLAGES:\n\n";
foreach (array_slice($villages, 0, 5) as $village) {
echo $village['id']
. ' - '
. $village['name']
. PHP_EOL;
}
# -----------------------------------
# Select village
# -----------------------------------
$addr->selectVillage($villages[0]['id']);
# -----------------------------------
# Final address
# -----------------------------------
echo "\nFORMATTED ADDRESS:\n\n";
echo $addr->getFormattedAddress();
echo "\n\n";
# -----------------------------------
# Raw selection
# -----------------------------------
echo "RAW SELECTION:\n\n";
print_r(
$addr->getSelection()
);
use UgAddress\UgAddress;
$addr = new UgAddress();
$districts = $addr->getDistricts();
$counties = $addr->getCounties('32');
$subcounties = $addr->getSubcounties('69');
$parishes = $addr->getParishes('1546');
$villages = $addr->getVillages('9127');
$addr->selectDistrict('32');
$addr->selectCounty('69');
$addr->selectSubcounty('1546');
$addr->selectParish('9127');
$addr->selectVillage('57217');
$addr->onDistrictChange(function ($e) {
print_r($e);
});
[
'districtId' => '32',
'counties' => [...]
]
$addr->onCountyChange(function ($e) {
print_r($e);
});
$addr->onSubcountyChange(function ($e) {
print_r($e);
});
$addr->onParishChange(function ($e) {
print_r($e);
});
$addr->onVillageChange(function ($e) {
print_r($e);
});
$selection = $addr->getSelection();
[
'district' => [
'id' => '32',
'name' => 'KAMPALA'
],
'county' => [
'id' => '69',
'name' => 'KAMPALA CENTRAL DIVISION'
],
'subcounty' => [
'id' => '123',
'name' => 'CENTRAL WARD'
],
'parish' => [
'id' => '456',
'name' => 'NAKASERO PARISH'
],
'village' => [
'id' => '789',
'name' => 'NAKASERO VILLAGE A'
]
]
echo $addr->getFormattedAddress();
$district = $addr->findDistrict('32');
$county = $addr->findCounty('69');
$subcounty = $addr->findSubcounty('1546');
$parish = $addr->findParish('9127');
$village = $addr->findVillage('57217');
$addr->reset();
use UgAddress\UgAddress;
Route::get('/districts', function () {
$addr = new UgAddress();
return response()->json(
$addr->getDistricts()
);
});