/    Sign up×
Community /Pin to ProfileBookmark

php-mysql array, autopopulating select statement options

Sorry to have to ask this, but I’ve been searching for quite some time, and am close to getting this to work but I just can’t do it. What I’m trying to do is new to me. hehe

I want to have a select statement in a form, with it’s options being populated by a field in a mysql table. I’m also going to have it check to see if one of the options should be selected, based on a field value in the form, and populate that option with a selected=”selected” attribute. Right now, it IS working, but I’m getting double options (2 options for every record in the mysql field).

Here is the code:

[code=php]
$officeresult = mysql_query(“SELECT `field` FROM `Table`”);
while ($row = mysql_fetch_array($officeresult)) {
$officename = $row[“field”];
foreach($row as $officevalue)
{
$officeselected = ($formItem == $officevalue) ? ‘ selected=”selected”‘ : “”;
echo “<option$officeselected>$officevalue</option>n”;
} }[/code]

like I said…it’s working “as intended,” but it’s creating double the options…

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@chazzyDec 14.2005 — I would try this:

[code=php]
$officeresult = mysql_query("SELECT field FROM Table");
while ($row = mysql_fetch_array($officeresult)) {
$officename = $row["field"];
$officeselected = ($formItem == $officename) ? ' selected="selected"' : "";
echo "<option$officeselected>$officename</option>n";
} [/code]


i don't see a reason for the foreach loop. you might also want to double check your query on the database to verify that it's not giving you double the results.
×

Success!

Help @rpassmor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...