Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Large Drop Down Box value Selected by Table 2

Status
Not open for further replies.

Muppsy007

Programmer
Apr 4, 2003
98
NZ
Hi,

I've got a free hyperbaric search database where people can look for a Dive chamber close to them. I'm in the process of making an update form where staff can login and update contact details held on our database.

When they login, a form opens with all of their details pre-entered into text feilds from the MySQL database. Easy enough. But I'm just having trouble working out how to pre-select the country using the value in the DB when the dropdown has around 200 hardcoded values.

Ok, I've done this before with a Gender and Title drop down, where it was easy to just do the following:

Code:
<select name="title" id="title" >
                                    <OPTION value=""> </option>
                                    <OPTION value="Mr"<? if (isset($recordset['title']) and $recordset['title'] == 'Mr') echo 'selected'?>>Mr</option>
                                    <OPTION value="Mrs"<? if (isset($recordset['title']) and $recordset['title'] == 'Mrs') echo 'selected'?>>Mrs</option>
                                    <OPTION value="Miss"<? if (isset($recordset['title']) and $recordset['title'] == 'Miss') echo 'selected'?>>Miss</option>
                                    <OPTION value="Ms"<? if (isset($recordset['title']) and $recordset['title'] == 'Ms') echo 'selected'?>>Ms</option>
                                    <OPTION value="Dr"<? if (isset($recordset['title']) and $recordset['title'] == 'Dr') echo 'selected'?>>Dr</option>
                                  </select>

Obviously this approach may be a bit tedious with 200 values. Is there an easier way? Am I better off loading all my countries into a table and do a loop to find the selected value? I don't want to use a text feild here, as we want to control the country names that are displayed in the search listings.

Thanks for any help
Aaron
 
You're better off putting the country names in a table and looping. For that matter, I'd have put the honorifics in a table, too.

Your code for countries then becomes something like:

//assuming connection

$rh = mysql_query ("Some appropriate query for getting countries");
while ($row = mysql_fetch_assoc($rh))
{
print '<option value="' . $row['country_column'] . '"';

if ($row['country_column'] == '<some previously selected value>')
print ' SELECTED';
print '>';
}


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Why don't you just build the entire dropdown dynamically?
It will contain all values, and only require a few lines of code.
Write a SQL statement that extracts all the countries and populate the <option> tags with that info. The comparison to the selected value remains the same.
 
Thanks alot guys. Will do that.

Sorry for the lateness of my reply. Been on holiday for a week.

Thanks again. Database it is.

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top