I have a script that has a list of country ID codes as $item_details[countries] which are coma delimited.
In order to display the names of the countries (stored in mySQL), it needs to currently run each loop to get the country name and then display it.
To prevent the script from connecting to the mySQL for each and every single name, could there be an easier way of getting all the names at once and then displaying them in a loop afterwards, so that there is only 1 mySQL query?
Such as:
$all_country_names = $db->get_sql_field("SELECT name,id FROM db_countries");
Here is what I have now:
// $item_details['countries'] are the coma delimited country IDs
// Changing the coma delimited IDs into an array
$array = explode(',', $item_details['countries']);
// Running the loop
foreach($array as $key=>$value){
// Getting each country name from the database based on the ID
$country_name = $db->get_sql_field("SELECT name FROM db_countries WHERE id='".$value."'");
// Displaying each country name
echo $country_name;
echo ", ";
}
Thank you in advance for any help :)