Hi Johanafm,
Thanks again for your reply. My first impression on seeing your code was to think that i've got no chance of understanding it - it's probably way too well written for a Newbie like me to understand. I was hoping for some minor modifications to my version (maybe not elegant but at least i'd be able to follow it). I thought i'd try to see if at least i could get your suggestion to work but came unstuck fairly quickly with the following message:
Warning: Invalid argument supplied for foreach() in C:\InetPub\wwwroot\test\myexample.php on line 106
I have 3 tables:
tbl_continent (continent_id, continent)
tbl_country (country_id, continent_id, country)
tbl_city (city_id, country_id, city)
and my version of the script you supplied (i stripped out the comment lines, added missing <html> and <body> tags and replaced several ines with what i thought the queries on the tables should be):
<html>
<head>
<script type="text/javascript">
function select_changed(o) {
if (o.value != 0) {
o.parentNode.parentNode.submit();
}
}
</script>
</head>
<body>
<?php
$continents = "SELECT continent_id, continent FROM tbl_continent ";
if (isset($_GET['submit_btn'])) {
$continent = (int) $GET['continent'];
$country = (int) $GET['country'];
$city = (int) $_GET['city'];
$continent_select =
'<select name="continent" onchange="select_changed(this);">
<option value="0">Please select...</option>';
foreach($continents as $k => $v) {
$continent_select .=
"\n\t<option value='$k'>$v</option>";
}
$continent_select .= "\n</select>";
$country_select = $city_select = '';
}
elseif (isset($_GET['country'])) {
$continent = (int) $GET['continent'];
$country = (int) $GET['country'];
$continent_select =
'<select name="continent" onchange="select_changed(this);">
<option value="0">Please select...</option>';
foreach ($continents as $k => $v) {
$continent_select .=
"\n\t<option value='$k'". ($continent == $k ? ' selected="selected"' : '') .">$v</option>";
}
$continent_select .= "\n</select>";
$all_countries = "SELECT country_id, country FROM tbl_country";
$countries = $all_countries[$continent];
$country_select =
'<select name="country" onchange="select_changed(this);">
<option value="0">Please select...</option>';
foreach ($countries as $k => $v) {
$country_select .=
"\n\t<option value='$k'". ($country == $k ? ' selected="selected"' : '') .">$v</option>";
}
$country_select .= "\n</select>";
$all_cities = "SELECT city_id, city FROM tbl_city";
$cities = $all_cities[$continent][$country];
$city_select =
'<select name="city">
<option value="0">Please select...</option>';
foreach ($cities as $k => $v) {
$city_select .=
"\n\t<option value='$k'>$v</option>";
}
$city_select .= "\n</select>\n<input type='submit_btn' name='submit' value='Submit'/>";
}
elseif (isset($_GET['continent'])) {
$continent = (int) $_GET['continent'];
$continent_select =
'<select name="continent" onchange="select_changed(this);">
<option value="0">Please select...</option>';
foreach ($continents as $k => $v) {
$continent_select .=
"\n\t<option value='$k'". ($continent == $k ? ' selected="selected"' : '') .">$v</option>";
}
$continent_select .= "\n</select>";
$all_countries = "SELECT * FROM tbl_country";
$countries = $all_countries[$continent];
$country_select =
'<select name="country" onchange="select_changed(this);">
<option value="0">Please select...</option>';
foreach ($countries as $k => $v) {
$country_select .=
"\n\t<option value='$k'>$v</option>";
}
$country_select .= "\n</select>";
$city_select = '';
}
else {
$continent_select =
'<select name="continent" onchange="select_changed(this);">
<option value="0">Please select...</option>';
foreach($continents as $k => $v) {
$continent_select .=
"\n\t<option value='$k'>$v</option>";
}
$continent_select .= "\n</select>";
$country_select = $city_select = '';
}
?>
<form action="" method="get">
<div>
<?php
echo $continent_select . PHP_EOL;
echo $country_select . PHP_EOL;
echo $city_select . PHP_EOL;
?>
</div>
</form>
<body>
<html>
Thanks