Hoi, Hallo,

Allereerst bedankt als je me wilt helpen met het volgende probleem. Wellicht zit ik er helemaal naast...

Ik heb een stukje code geschreven om van alle landen over de wereld de bijbehorende provincies, districten, counties etc. erbij te zetten. Nu is het geval dat ik bij een land de districten onder hun Counties wil plaatsen. Hiervoor hebben de Counties een vaste code en de Districten heb ik zo gemaakt dat ze de bij behorende County codes bevatten. Echter is het probleem nu dat ik het niet voor elkaar krijg om de Districten onder de juiste County te zetten.

Dit is wat ik wil:
County 1
- District 1
- District 3
- District 6

County 2
- District 2
- District 4
- District 5

etc.

Maar ik krijg nu dit (voorbeeld, niet exacte namen):
County 1
County 2
District 1
District 2
District 3
District 4
District 5
District 6

Script hieronder laat de generated codes achter de Districten zien:

echo $row['subdivision_name']." - <i>".$row['category']."</i><br />";
$fixed = $row['subdivision_code'];
$generated = "";
//$generated = $row['alpha2']."-".$row['subdivision_parent'];
//echo "generated: ".$generated." <br /> ";
if (!empty($row['subdivision_parent'])) {
$generated = $row['alpha2']."-".$row['subdivision_parent'];
$fixed = "";
echo "generated: ".$generated." <br /> ";
}

Heeft iemand een idee hoe ik dit kan aanpakken?
Indien meer info nodig is hoor ik het graag.
Dit heeft te maken met character-encoding in het forum, waarbij tijd moet worden gevonden om te fixxen.

Hoe heb je nu in je site UTF-8 ingesteld?
- Ariën - data word in de database op UTF-8 gezet, bij mij is de COLLATION ingesteld op: utf8_unicode_ci
Vervolgens zet ik in PHP de character set op UTF-8:

<?php
// Change character set to utf8
if (!$conn->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n<br />", $conn->error);
} else {
printf("Current character set: %s\n<br />", $conn->character_set_name());
}
?>

Ik hoop dat dat je helpt!
Met dit krijg je het lijstje zoals gewenst.
Ik gebruik object ipv assoc, dus effe aanpassen.
En ook de category aanpassen, die veldnaam heb ik niet, vandaar 'district'.

<?php
$check_previous = "";
while( $row = $result->fetch_object() )
{    
	$shortname = $row->Name;
	if ( $shortname <> $check_previous )
	{
		echo '<b>' . $shortname . '</b><br />';
		$check_previous = $shortname;
	}
	// Put Districts (with Parent Codes) in County
	echo '&nbsp;&nbsp;' . $row->Subdivision . ' - <i>District</i><br />';
	if ( !empty( $row->SubCode ) )
	{
		$generated = $row->SubCode;
		echo '&nbsp;&nbsp;Generated : ' . $generated . '<br />';
	}    
}
?>
Adoptive Solution, is het mogelijk om jouw complete werkende code (inclusief de SQL statement) hier te plaatsen?
Wat ik ook probeer met jou code... ik krijg het niet voor elkaar dat het werkt. Ik krijg eerst alle counties en daarna de districten. Dus niet de counties met de bij behorende districten. Wellicht doe ik iets fouts, vandaar dat ik graag je volledig werkende code zou willen inzien. Bij voorbaat dank!
komt ie :

Een selectie data voor de landen :

DROP TABLE IF EXISTS `iso3166_1`;
CREATE TABLE `iso3166_1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `numeric` int(11) DEFAULT NULL,
  `Alpha2Code` varchar(4) DEFAULT NULL,
  `Alpha3Code` varchar(4) DEFAULT NULL,
  `CountryName` varchar(128) DEFAULT NULL,
  `TopLevelDomain` varchar(5) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `numeric` (`numeric`),
  KEY `Alpha2Code` (`Alpha2Code`),
  KEY `CountryName` (`CountryName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `iso3166_1` (`id`, `numeric`, `Alpha2Code`, `Alpha3Code`, `CountryName`, `TopLevelDomain`) VALUES
(1,	20,	'ad',	'and',	'Andorra',	'.ad'),
(2,	4,	'af',	'afg',	'Afghanistan',	'.af'),
(3,	28,	'ag',	'atg',	'Antigua and Barbuda',	'.ag'),
(4,	660,	'ai',	'aia',	'Anguilla',	'.ai'),
(5,	8,	'al',	'alb',	'Albania',	'.al');


en de regios :

DROP TABLE IF EXISTS `iso3166_2`;
CREATE TABLE `iso3166_2` (
  `country_code` char(2) DEFAULT NULL,
  `subdivision_name` varchar(128) DEFAULT NULL,
  `subdivision_code` varchar(10) DEFAULT NULL,
  KEY `idx_country_code` (`country_code`),
  KEY `idx_region_name` (`subdivision_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `iso3166_2` (`country_code`, `subdivision_name`, `subdivision_code`) VALUES
('AD',	'Andorra la Vella',	'AD-07'),
('AD',	'Canillo',	'AD-02'),
('AD',	'Encamp',	'AD-03'),
('AD',	'Escaldes-Engordany',	'AD-08'),
('AD',	'La Massana',	'AD-04'),
('AD',	'Ordino',	'AD-05'),
('AD',	'Sant Julia de Loria',	'AD-06'),
('AE',	'\'Ajman',	'AE-AJ'),
('AE',	'Abu Zaby',	'AE-AZ'),
('AE',	'Al Fujayrah',	'AE-FU'),
('AE',	'Ash Shariqah',	'AE-SH'),
('AE',	'Dubayy',	'AE-DU'),
('AE',	'Ra\'s al Khaymah',	'AE-RK'),
('AE',	'Umm al Qaywayn',	'AE-UQ'),
('AF',	'Baghlan',	'AF-BGL'),
('AF',	'Balkh',	'AF-BAL'),
('AF',	'Bamyan',	'AF-BAM'),
('AF',	'Faryab',	'AF-FYB'),
('AF',	'Helmand',	'AF-HEL'),
('AF',	'Herat',	'AF-HER'),
('AF',	'Kabul',	'AF-KAB'),
('AF',	'Kandahar',	'AF-KAN'),
('AF',	'Khost',	'AF-KHO'),
('AF',	'Kunduz',	'AF-KDZ'),
('AF',	'Logar',	'AF-LOG'),
('AF',	'Nangarhar',	'AF-NAN'),
('AF',	'Nimroz',	'AF-NIM'),
('AF',	'Paktika',	'AF-PKA'),
('AF',	'Paktiya',	'AF-PIA'),
('AF',	'Parwan',	'AF-PAR'),
('AF',	'Takhar',	'AF-TAK'),
('AF',	'Uruzgan',	'AF-URU'),
('AG',	'Redonda',	'AG-11'),
('AG',	'Saint George',	'AG-03'),
('AG',	'Saint John',	'AG-04'),
('AG',	'Saint Mary',	'AG-05'),
('AG',	'Saint Paul',	'AG-06'),
('AG',	'Saint Peter',	'AG-07'),
('AG',	'Saint Philip',	'AG-08'),
('AI',	'Anguilla',	'-'),
('AL',	'Berat',	'AL-01'),
('AL',	'Diber',	'AL-09'),
('AL',	'Durres',	'AL-02'),
('AL',	'Elbasan',	'AL-03'),
('AL',	'Fier',	'AL-04'),
('AL',	'Gjirokaster',	'AL-05'),
('AL',	'Korce',	'AL-06'),
('AL',	'Kukes',	'AL-07'),
('AL',	'Lezhe',	'AL-08'),
('AL',	'Shkoder',	'AL-10'),
('AL',	'Tirane',	'AL-11'),
('AL',	'Vlore',	'AL-12');


Een procedure die je in php aanroept :

DELIMITER ;;

DROP PROCEDURE IF EXISTS `iso3166`;;
CREATE PROCEDURE `iso3166`(IN `van` char(3), IN `tot` char(3))
SELECT
	UPPER(Alpha2Code) AS Code,
	CountryName AS Name,
	subdivision_code AS SubCode,
	subdivision_name AS Subdivision
FROM
	iso3166_1
LEFT JOIN
	iso3166_2
ON
	Alpha2Code = country_code
WHERE
	 Alpha2Code >= van AND Alpha2Code <= tot
ORDER BY
	Alpha2Code, Name, Subdivision;;

DELIMITER ;


En de webpagina :

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>ISO3166 List</title>

<meta name="viewport" content="user-scalable=1, width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0" />

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

</head>

<body>
<?php
//db details
$dbHost     = 'localhost';
$dbUsername = 'user';
$dbPassword = 'password';
$dbName     = 'wereld';

//Connect and select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$db->set_charset("utf8mb4");
$db->query("SET NAMES utf8mb4 COLLATE utf8mb4_general_ci");

if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}
?>
<?php

$Alpha2CodeFrom = strtoupper( filter_var( $_GET['van'], FILTER_SANITIZE_STRING ) );
$Alpha2CodeTo   = strtoupper( filter_var( $_GET['tot'], FILTER_SANITIZE_STRING ) );

if ( $Alpha2CodeFrom > $Alpha2CodeTo )
{
	$Alpha2CodeTemp = $Alpha2CodeFrom;
	$Alpha2CodeFrom = $Alpha2CodeTo;
	$Alpha2CodeTo   = $Alpha2CodeTemp;
}

$query = "CALL iso3166( '" . $Alpha2CodeFrom . "', '" . $Alpha2CodeTo . "')";

if ( $result = $db->query($query) )
{
	if ( $result->num_rows > 0 )
	{
		$check_previous = "";
		while( $row = $result->fetch_object() )
		{    
			$shortname = $row->Name;
			if ( $shortname <> $check_previous )
			{
				echo '<b>' . $shortname . '</b><br />';
				$check_previous = $shortname;
			}
			// Put Districts (with Parent Codes) in County
			echo '&nbsp;&nbsp;' . $row->Subdivision . ' - <i>District</i><br />';
			if ( !empty( $row->SubCode ) )
			{
				$generated = $row->SubCode;
				echo '&nbsp;&nbsp;Generated : ' . $generated . '<br />';
			}    
		}
	}
}
?>

	</div>

</body>

</html>


De pagina roep je aan als volgt :

pagina.php?van=af&tot=af



[size=xsmall]Toevoeging op 08/04/2019 11:42:45:[/size]

Werkend voorbeeld :

http://adoptive.2kool4u.net/dynamic_select/iso3166.php?van=af&tot=am
Adoptive Solution, bedankt voor je snelle reactie. Ik heb je script exact overgenomen en in mijn browser laten draaien. Werkt op zich goed, maar ik mis wat...

Dit is wat ik nu krijg als ik pagina.php?van=al&tot=al doe:

Albania
Berat - District
Generated : AL-01
Diber - District
Generated : AL-09
Durres - District
Generated : AL-02
Elbasan - District
Generated : AL-03
Fier - District
Generated : AL-04
Gjirokaster - District
Generated : AL-05
Korce - District
Generated : AL-06
Kukes - District
Generated : AL-07
Lezhe - District
Generated : AL-08
Shkoder - District
Generated : AL-10
Tirane - District
Generated : AL-11
Vlore - District
Generated : AL-12

Dat is inderdaad een lijst van Counties onder de Country. Maar als ik de Districten van Albania in de database erbij zet dan word dat er niet bij toegevoegd. Kan ook niet als ik zo naar je code kijk, er mis een stukje code waarin een subdivision_parent (generated) naar een subdivision_code (fixed) kijkt en de Districten onder de juiste Counties onderbrengt.

Wat het moet worden is dit:

Albania
Berat - County
Berat - District
Kuçovë - District
Skrapar - District
Durrës - County
Durrës - District
Krujë - District
Elbasan - County
Elbasan - District
Gramsh - District
Librazhd - District
Peqin - District
etc.

De Districten hebben een eigen subdivision_code die anders is dat de subdivision code die elk County heeft. Daarom heb ik een kolom erbij gezet: subdivision_parent daarin staan bij mij nu de subdivision_codes van de juiste County (parents) in bij de Districten.

Dus:

County AL-01 - Berat - County is een fixed subdivision_code
District - Berat heeft een fixed subdivision_code dat AL-BR is en een subdivision-code AL-01 om naar de County AL-01 te verwijzen.
District - Kuçovë heeft een fixed subdivision_code dat AL-KC is en een subdivision-code AL-01 om naar de County AL-01 te verwijzen.
County AL-02 - Durrës - County is een fixed subdivision_code
etc.
Dan pas je het aan!
Je mag ook zelf wel iets bedenken.
Adoptive Solution, ik begrijp je reactie niet goed omdat ik vanaf mijn eerste vraag hier naar vraag. Al dat andere had ik juist al zelf. Gezien het feit dat ik denk dat er wat misverstanden waren tussen County en Country en tussen wat fixed en generated betekende in mijn codes probeer ik mij hier meer mijn probleem uit te leggen.

Ikzelf dacht dat ik met fixed en generated deze parent en child codes aan elkaar kon koppelen, maar dat is dus niet het geval. Ik heb mijn uitleg hierboven nog wat toegelicht een gewijzigd dus wellicht iets beter uitgelegd nu.

Adoptive Solution, ik waardeer alle hulp van jou en van anderen dus begrijp mij niet verkeerd. En ik denk echt wel mee ;-)
Kijk eens met print_r naar je boom met data. Met een if-je kan je alle Counties dan wel vetgedrukt maken.

Reageren