Destramic Posted May 5, 2010 Share Posted May 5, 2010 hey...i've just made a table that looks like this called COUNTRYS counrty_id continent country_name ------------------------------------------------------- 1 Europe United Kingdom 2 Europe Netherlands 3 Asia Thailand -------------------------------------------------------- now i know this is possible but i was to display the results like this if anyone can help plese Europe- United Kingdom Nethlands Asia- Thailand thanks Quote Link to comment https://forums.phpfreaks.com/topic/200841-help/ Share on other sites More sharing options...
Destramic Posted May 6, 2010 Author Share Posted May 6, 2010 anyone please? Quote Link to comment https://forums.phpfreaks.com/topic/200841-help/#findComment-1054206 Share on other sites More sharing options...
Daz84 Posted May 6, 2010 Share Posted May 6, 2010 Visit this page to learn about displaying the output of a table http://www.w3schools.com/PHP/php_mysql_select.asp Basing your code on what you learn on that page, you will need to nest a loop inside a loop. The outer loop will find exclusive continents and the inner loop will display all the countries for that continent. Normally I would suggest creating a lookup table for the outer loop which would reduce the amount of coding required significantly but in this specific case you know the number of continents is not going to change so you could simply hard code it using a loop or even a function which could be called for each continent. If you choose to do that it would probably look something like this function listCountries($strContinent) { echo $strContinent . "-<br />"; $result = mysql_query("SELECT country_name FROM countrys WHERE continent = " . $strContinent); while($row = mysql_fetch_array($result)) { echo $row['country_name']; echo "<br />"; } echo "<p />"; } ListCountries("Europe"); ListCountries("Asia"); Although I'd like it to be known a far neater solution would be to have a second table with a list of continents which would be looked up in a loop and the ListCountries function called for each one instead of simply writing out the function call however many times. I have also included your spelling error in the sql query to prevent the need for you to change the name of your table. I notice it says "countrys" instead of "countries" Hope this post has been of some help to you, don't hesitate to ask if you need any further help Daz Quote Link to comment https://forums.phpfreaks.com/topic/200841-help/#findComment-1054225 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2010 Share Posted May 6, 2010 Please don't put queries inside of loops. Take a look at the $last_tab logic in this thread (you would change it to store the last continent value) - http://www.phpfreaks.com/forums/index.php/topic,296615.msg1405059.html#msg1405059 Simply use one query to retrieve the data you want in the order that you want it. Then in your presentation code, detect when the continent value changes to output a new heading and remember the new continent value. Quote Link to comment https://forums.phpfreaks.com/topic/200841-help/#findComment-1054226 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.