/    Sign up×
Community /Pin to ProfileBookmark

Populate data from 2 different tables into one listbox

Hi all,
I need to populate the country data and city data into one listbox.
I have a table name country, and table city with country id.
I have no idea how to make it.
I believe there is something to do with php code too.
But i have no picture yet.
Please show me the way.
Thanks in advanced.

the listbox should b like this:

Malaysia
->Melaka
->Selangor
->Sabah
Thailand
->Bangkok
->Chiang Mai
->Chiang Rai

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ahfan85authorJul 20.2011 — Hi all,

I need to populate the country data and city data into one listbox.

I have a country table, and a city table with country id.

I have no idea how to make it.

Please show me the way.

Thanks in advanced.

the listbox should b like this:

Malaysia

->Melaka

->Selangor

->Sabah

Thailand

->Bangkok

->Chiang Mai

->Chiang Rai
Copy linkTweet thisAlerts:
@slyfoxJul 20.2011 — if you don't find an answer here... try posting your question in the html section
Copy linkTweet thisAlerts:
@NogDogJul 20.2011 — Making guesses at table names/columns, so you'll have to fix those:
[code=php]
<?php
$sql = "
SELECT
country.name AS country_name,
city.name AS city_name,
city.id AS city_id
FROM country
INNER JOIN city ON city.country_id = country.id
ORDER BY country_name, city_name
";
$result = mysql_query($sql);
$countries = array();
while($row = mysql_fetch_assoc($result)) {
$countries[$row['country_name']][] = array(
'id' => $row['city_id'],
'city' => $row['city_name']
);
}
echo "<select name='city'>n";
foreach($countries as $country => $cities) {
printf("<optgroup label='%s' />n", htmlspecialchars($country));
foreach($cities as $city) {
printf(
"<option value='%d'>%s</option>n",
$city['city_id'],
htmlspecialchars($city['city_name'])
);
}
}
echo "</select>n";
[/code]
Copy linkTweet thisAlerts:
@JMRKERJul 20.2011 — Create an array with your countries and cities like this...
<i>
</i>var country_city = [
['Malaysia', [ 'Melaka', 'Selangor', 'Sabah' ] ],
['Thailand', [ 'Bangkok', 'Chiang Mai', 'Chiang Rai'] ]
];


To be used something like this ...
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;script type="text/javascript"&gt;
var country_city = [
[ 'Malaysia', [ 'Melaka', 'Selangor', 'Sabah' ] ],
[ 'Thailand', [ 'Bangkok', 'Chiang Mai', 'Chiang Rai'] ]
];
for (var i=0; i&lt;country_city.length; i++) {
alert(country_city[i][0]+'nt'+country_city[i][1].join('nt'));
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;

From this set-up we should be able to populate (create) the 2 drops downs lists.
×

Success!

Help @ahfan85 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...