Store MySQL in PHP array for two queries
How do I create, save and output an array that uses two different mysql queries?
I tried to make a simple example.
$select1 = "SELECT country_id, country_name FROM countries ...";
while ($select1) {
...store country results in array...
$select2 = "SELECT city_id, city_name FROM cities where '" . $select1['country_id'] . "'..."); // depends on select1
while ($select2) {
...store city results in array...
}
}
**output something like this:**
country_id = 1
country_name = United States
city_id = 1
city_name = New York
city_id = 2
city_name = Las Vegas
country_id = 2
country_name = Canada
city_id = 3
city_name = Ottawa
+1
source to share
3 answers
I don't know if you are checking for bugs, preparing or avoiding your requests, but please do so.
To create an array, you can do it with this:
$list = [];
$countries = $link->query("SELECT country_id, country_name FROM countries ...");
while ($country_row /*fetch from $countries*/) {
$country_id = $country_row['country_id'];
$country_info = [
'country_id' => $country_id,
'country_name' => $country_row['country_name'],
'country_cities' => []
];
$cities_stmt = "SELECT city_id, city_name FROM cities where $country_id...";
$cities = $link->query($cities_stmt);
while ($city_row /*fetch from $cities*/) {
$city_id = $city_row['city_id'];
$country_info['country_cities'][$city_id] = [
'city_id' => $city_id,
'city_name' => $city_row['city_name']
];
}
$list[$country_id] = $country_info;
}
To display your array, you can do:
foreach ( $list as $country_id => $country_info ) {
echo "Country ID: $country_id<br />";
echo 'Country Name: ' . $country_info['country_name'] . '<br />';
echo 'Country Cities:<br />';
$cities = $country_info['country_cities'];
foreach ( $cities as $city_id => $city_info ) {
echo " City ID: $city_id<br />";
echo ' City Name: ' . $city_info['city_name'] . '<br />';
}
echo '<br />';
}
Also, if you know the id of the country or city you can do:
echo 'City Name: ' . $list[$country_id]['country_cities'][$city_id]['city_name'] . '<br />';
+1
source to share
Use your country_id as the key for your array. So basically,
$select1 = mysqli_query("SELECT country_id, country_name FROM countries");
while ($country_row = mysqli_fetch_array($select1, MYSQLI_ASSOC)) {
$array[$country_id]['country_id'] = $country_row ['country_id'];
$array[$country_id]['country'] = $country_row['country_name'];
$select2 = mysqli_query("SELECT city_id, city_name FROM cities where '" . $select1['country_id'] ."' "); // depends on select1
while ($city_row = mysqli_fetch_array($select2, MYSQLI_ASSOC)) {
$array[$country_id]['cities'][]['city_id'] = $city_row['city_id'];
$array[$country_id]['cities'][]['city_name'] = $city_row['city_name']
}
}
To print them back using foreach (as you asked in the comments) you have to go through both levels of the array
foreach($array as $country){
//Loop through the countries that we queried and stored
echo 'Country Name: '.$country['country_name'].'<br/>';
echo 'Country ID: '.$country['country_id'].'<br/>';
echo 'Cities in country: <br/>';
//Loop through the cities within this country
foreach($country['cities'] as $city){
echo 'City ID: '.$city['city_id'].'<br/>';
echo 'City Name: '.$city['city_name'].'<br/>';
}
}
0
source to share
Doing the output of one request and emailing it to another request in a nested loop is an anti-pattern. Use a connection:
select country_ID, country_name,
City_Id, city_name
From countries cn
Inner join cities ct
On CT.country_id = cn.country_ID
Then....
$cities=array();
$country_names=array();
While($r=mysqli_fetch_array($handle)) {
$country_names[$r['country_id']]=$r['country_name'];
If (!is_array($cities[$r['country_id'])) $cities[$r['country_id']]=array();
$cities[$r['country_id']][$r['city_id']]=$r['city_name'];
}
0
source to share