Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
| Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 5th, 2005, 11:07 PM
Registered User
 
Join Date: Feb 2005
Location: Tempe, AZ, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Iteration through a dropdown.

Hi there!

I'm new to PHP and I'm having trouble getting past this obstacle:

We're using a MySQL database and there is a particular field within a table that I'd like to compare to a dropdown. for example..

The field within the table is "state". It can contain the two letter abbreviation for any of the 50 states. On the page is a dropdown listing all of the 50 states and the values of the options within the dropdown are the two digit abbreviation of the states. When the page loads and pulls data from the database and populates editable fields, such as other text boxes, etc. How can I iterate through the dropdown and have the one that matches the entry within the database be "selected"?

Your time is appreciated. Thanks!

-Ben

 
Old February 9th, 2005, 11:05 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

List the states like this:
Code:
$states_abbr = array("AB","AL", etc...);
$states = array("Alabama","Alaska", etc...);
echo "<select attributes...>";

for($i=0;$i<count($states);$i++)
{
    echo "<option value='$states_abbr[$i]' ".(($database_data['state_abbr']==$states_abbr[$i])? "selected" : "").">$states[$i]</option>";
}

echo "</select>";
hth,

-Snib - http://www.snibworks.com
Where will you be in 100 years?




Similar Threads
Thread Thread Starter Forum Replies Last Post
iteration bostek Excel VBA 5 August 30th, 2006 01:23 AM
iteration movenext weazy Excel VBA 0 June 9th, 2006 05:56 PM
Picture iteration bahachin Excel VBA 3 September 28th, 2005 12:44 PM
Iteration in struts sridevi Servlets 2 July 1st, 2005 07:01 AM
file/directory iteration nbryson C# 5 August 1st, 2003 05:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.