Hello,

I actually have a rather "stupid" problem but i cant get it solved. I have in my database a table wich is called country and where all the countries are kept in with their iso code and name. I wish to read all the data from that table so i can fill up my option value in html code.

The way i think to do it:

"This is done at the top of the page"

 $results = db_query("SELECT * FROM country");

"This is what i try to do at my html code"

 	       for($i = 0; $i < db_num_rows($results); $i++)
		{												
			 $result = mysql_result($results, $i);
	                 echo "<option value=" . $xxxxx. " $selected>" .   $xxxxxxx . "</option>";
		}				

As you can see im a bit confused what to do with my option value's and this for a simple reason. When i do echo sizeof($results) i get 1 but when i do db_num_rows i get 239. So i wonder if i only got the first column from my database? And suppose i get all the data correctly how do i tell drupal that i want for example the second column of data in my array to be my option value and the third column to be the name that gets displayed as a option.

I hope this information is sufficient for solving my problem.

Thx in advance

Comments

clivesj’s picture

Assuming:
your table name is {country} ... always put table name between {} when performing the sql
database column for name = name
database column for code = iso_code

$results = db_query("SELECT * FROM {country}");
  while ($result = db_fetch_array($results)) {
    echo 'Country name = ' . $result[]['name'] . 'Country code = ' . $result[]['iso_code'];
}

warning: UNTESTED!

Smontje’s picture

Thank you clivesj i will give it a try

Edit:

while ($result = db_fetch_array($results)) {
echo "<option value=" . $result[]['iso'] . ">" . $result[]['name'] . "</option>";}			

I echo'ed it your code and it works but the problem is that i cant get this value echo'ed it shows nothing...

clivesj’s picture

sorry, try this first just to verify the query results can be displayed:

echo $result[iso]  . " / "  . $result[name] ;}
  

just to see if the names are echoed.
And then, are you trying th output an itemlist or so? I don't get what you are trying to display

Smontje’s picture

Well to clearify:

I want to have a list selection where people can choose from wich country they are from on my custom registration page. Therefore i want that the names of every country that is listed in my database to show up on my list.

clivesj’s picture

Does the code above echo your db results?

Smontje’s picture

Yes it does it correctly, so im guessing my error is when trying to echo my option value's?

Edit: It finally worked thank you !!!!!!