53 lines
1.9 KiB
PHP
53 lines
1.9 KiB
PHP
<?php
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
|
|
|
|
if(isset($_REQUEST['temp_region'])) {
|
|
$temp_region = $_REQUEST['temp_region'];
|
|
|
|
function buildTree(array $elements, $parentId = 0) {
|
|
$branch = array();
|
|
foreach ($elements as $element) {
|
|
|
|
if ($element['parent_id'] == $parentId) {
|
|
$children = buildTree($elements, $element['id']);
|
|
if ($children) {
|
|
$element['children'] = $children;
|
|
}
|
|
$branch[] = $element;
|
|
}
|
|
}
|
|
|
|
return $branch;
|
|
}
|
|
|
|
$sql = "SELECT DISTINCT regions.* FROM regions
|
|
INNER JOIN blocks b ON b.region_id = regions.id OR regions.parent_id is null
|
|
INNER JOIN apartments a ON a.block_id = b.id
|
|
WHERE regions.rf_region_id = $temp_region
|
|
AND (regions.domclick_id IS NOT NULL )"; // ORDER BY name // AND regions.parent_id IS NOT NULL
|
|
$rez = mysql_query($sql);
|
|
|
|
while ($row = mysql_fetch_assoc($rez)) {
|
|
$regions[] = $row;
|
|
}
|
|
|
|
$treeRegions = buildTree($regions);
|
|
|
|
foreach ($treeRegions as $item) {
|
|
if (!$item['children']) {
|
|
$className = 'item';
|
|
$selectedStr = (isset($get['district']) && $get['district'] && in_array($item['id'], $get['district'])) ? "selected" : '';
|
|
echo "<option class='$className' value='$item[id]' $selectedStr>$item[name]</option>";
|
|
} else {
|
|
echo "<optgroup label='$item[name]'>";
|
|
foreach ($item['children'] as $subItem) {
|
|
$className = !$subItem['parent_id'] ? 'parent' : 'item';
|
|
$selectedStr = (isset($get['district']) && $get['district'] && in_array($subItem['id'], $get['district'])) ? "selected" : '';
|
|
echo "<option class='$className' value='$subItem[id]' $selectedStr>$subItem[name]</option>";
|
|
}
|
|
}
|
|
|
|
echo "</optgroup>";
|
|
}
|
|
}
|