Fix bugs in location code

This commit is contained in:
2023-02-24 13:39:05 +01:00
parent 0696a16030
commit 02b3cfd23f
4 changed files with 26 additions and 25 deletions

View File

@@ -16,35 +16,33 @@ include("header.php");
// ************* <option value="0">{$lang_option_none}</option>
$sql = "SELECT location_id AS id, location_name, location_parent, location_sort
FROM location
ORDER BY location_parent, location_sort, location_name";
$sql = "SELECT location_id AS id, location_name AS name,
location_parent AS parent, location_sort AS sort
FROM location
ORDER BY location_parent, location_sort, location_name";
$sth = $dbh->query($sql);
$locations = $sth->fetchAll();
$location_counter = count($locations);
if ($location_counter > 0) {
foreach ($locations AS $location) {
$location_names[$location['location_id']] = $location['location_name'];
$parents[$location['location_parent']][] = $location['location_id'];
}
if (count($locations) > 0) {
foreach ($locations AS $location) {
$location_names[$location['id']] = $location['name'];
$parents[$location['parent']][] = $location['id'];
}
}
// look for parents
// function to look for parents and create a new array for every child
function location($parents, $parent = 0) {
foreach ($parents[$parent] as $child) {
if (isset($parents[$child])) {
// element has children
$children[$child] = location($parents, $child);
} else {
// no children, set NULL
$children[$child] = NULL;
}
}
return $children;
foreach ($parents[$parent] as $child) {
if (isset($parents[$child])) {
// element has children
$children[$child] = location($parents, $child);
} else {
// no children, set NULL
$children[$child] = NULL;
}
}
return $children;
}
// recursive children check to template
@@ -63,8 +61,11 @@ function checkchildren($locations, $level) {
}
$tree = location($parents);
// create tree option list
$location_options = array(0 => '-');
checkchildren($tree, 0);
$smarty->assign("location_options", $location_options);
$smarty->assign("location_parent", $location_parent);