Jump to content

help!


Destramic

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.