/    Sign up×
Community /Pin to ProfileBookmark

using an array with an if statement

Hi, i am using this code below to generate my drop down box (all the select statements and everything are fine)

[code=php]
<select name=”Type” id=”Type”>
<option>Please Select</option>
<?PHP
//fill in the categories
while ($row = mysql_fetch_array($type_res)) {
extract($row);
echo “<option value=’$Type’>$Type</option>n”;
}
?>
</select>
[/code]

This works fine it populates the drop down box and everything its great.

what i have done below is put in an if script where if $Sub_Type is equal to $Type then it will echo selected so that, it will show what is in the database

so u know $Type is pulling from a different database to $Sub_Type and $Type is only to populate the drop down. what is happenen is i have a query to retrieve $sub_type from a product database and this is for an edit screen. so i need them to be able to change it but only to things populated from the $Type

[code=php]
<option>Please Select</option>
<?PHP
//fill in the categories
while ($row = mysql_fetch_array($type_res)) {
extract($row);
echo “<option value=’$Type’ <?PHP if($Sub_Type==$Type) echo”selected”;>?>$Type</option>n”;
}
?>
</select>
[/code]

This code works when i am not doin an arrary but the array messes it up i beleive becuase of the echo coz i usualy do this

[code=php]
<select name=”sex”>
<option value=”Male” <?php if($sex==”Male”) echo “selected”; ?>>Male</option>
<option value=”Female” <?php if($sex==”Female”) echo “selected”; ?>>Female</option>
</select>
[/code]

Any ideas y it aint working ?

Thanks Adam

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 09.2004 — You have embedded php tags within a php block. Try something like this instead:
[code=php]
<option>Please Select</option>
<?PHP
//fill in the categories
while ($row = mysql_fetch_array($type_res))
{
extract($row);
echo "<option value='$Type'";
if($Sub_Type==$Type)
{
echo "selected";
}
echo ">$Type</option>n";
}
?>
</select>
[/code]
Copy linkTweet thisAlerts:
@Paul_JrSep 10.2004 — Alternately, so you don't have that if statement breaking up your output (I bet I'm the only one who isn't okay with that ?):
[code=php]
<option>Please Select</option>
<?PHP
//fill in the categories
while ($row = mysql_fetch_array($type_res)) {
extract($row);
$selected = $Sub_Type == $Type ? ' selected' : '';
echo "<option value='$Type' $selected>$Type</option>n";
}
?>
</select>
[/code]

That way, if $Sub_Type doesn't equal $Type, the string is just empty, otherwise the value is ' selected'.
×

Success!

Help @k0r54 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 7.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ,
analytics: Fullres
});

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: @qewfun,
tipped: live stream
amount: 5000 SATS,

tipper: @qewfun,
tipped: live stream
amount: 5000 SATS,

tipper: @qewfun,
tipped: live stream
amount: 5000 SATS,
)...