Answer the question
In order to leave comments, you need to log in
How to remove elements from an array by groups?
There is a table of cities, they indicate countries in the form of id of these countries from the countries table. How can I display cities by country from an array. It should be like this:
Russia: Moscow, St. Petersburg....
Belarus: Minsk, Mogilev....
Kazakhstan: Astana, Temirtau....
It is desirable that the output be automatically recognized by the country id. In order not to manually check each country, in case new countries are added.
Answer the question
In order to leave comments, you need to log in
Make a selection from the database:
SELECT
country.name, cities.name
FROM
country
LEFT JOIN
cities
ON
country.id=cities.country_id
$query = "SELECT * from contry";
$res = mysql_query( $query );
while ( $item = mysql_fetch_array( $res ) )
{
if ($item['country.name']=="Россия") {$rus.= $item['city.name'].' '; };
if ($item['country.name']=="Беларусь") {$bel.= $item['city.name'].' '; };
if ($item['country.name']=="Казахстан") {$kz.= $item['city.name'].' '; };
}
echo 'Россия: '.$rus;
echo 'Беларусь: '.$bel;
echo 'Казахстан :'.$kz;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question