By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

Defualt Value for drop down list

P: 9
I have a drop down/list and am using a php and a mysql table to store my values. I am displaying an update page that already has the user's number stored in it. I want to default their current number value in the dropdown/list. How do I accomplish this? My code is below. What am I missing ?

<select name="number" class="longfield-select" option value="<?echo "$number?>">

<option value="">Select Number</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>

</select>
Feb 20 '08 #1
Share this Question
Share on Google+
7 Replies


TheServant
Expert 100+
P: 1,168
[HTML]<select name="number" class="longfield-select">

<option value="" <?php if ($number == NULL) { echo("selected=\"selected\""); } ?> >Select Number</option>
<option value="One" <?php if ($number == 1) { echo("selected=\"selected\""); } ?>>One</option>
<option value="Two" <?php if ($number == 2) { echo("selected=\"selected\""); } ?>>Two</option>
<option value="Three" <?php if ($number == 3) { echo("selected=\"selected\""); } ?>>Three</option>

</select>[/HTML]

I'm no pro, and I've probably made some mistakes just because I am saying this, but your php syntax is not very good. Even if you don't want to do what I wrote, just have a look at what is different between my php code and yours.

Regards,
The Servant
Feb 20 '08 #2

P: 9
Thanks for the feedback. However, I wouldn't of posted this question if I knew how to handle this.

Thanks
Feb 20 '08 #3

TheServant
Expert 100+
P: 1,168
Thanks for the feedback. However, I wouldn't of posted this question if I knew how to handle this.

Thanks
I meant no offense, I started teaching myself php about 3 months ago, so I have a little experience (most of it trial and error)! Is this what you wanted?
Feb 20 '08 #4

P: 9
Yes this will work. Thanks for your help
Feb 20 '08 #5

Markus
Expert 5K+
P: 6,050
Yes this will work. Thanks for your help
While theservants does indeed work, a more effective way would be to loop through the options and use an if to check - this way you arent opening and closing the php parser:
[php]
<select>
<option value="">Select one</option>
<?php
$_n = 4; // users selected number
for($_i = 1; $_i < 6; ++$_i)
{
if($_i == $_n) // if this is the users number...
echo "<option value=\"$_i\" selected=\"selected\">$_i</option>";
else
echo "<option value=\"$_i\">$_i</option>";
}
?>
</select>
[/php]
Feb 20 '08 #6

TheServant
Expert 100+
P: 1,168
While theservants does indeed work, a more effective way would be to loop through the options and use an if to check - this way you arent opening and closing the php parser:
[php]
<select>
<option value="">Select one</option>
<?php
$_n = 4; // users selected number
for($_i = 1; $_i < 6; ++$_i)
{
if($_i == $_n) // if this is the users number...
echo "<option value=\"$_i\" selected=\"selected\">$_i</option>";
else
echo "<option value=\"$_i\">$_i</option>";
}
?>
</select>
[/php]
Nice! I tried to think of a loop way but still very inexperienced when it comes to that. That's why I stick around here, always learning!
Feb 20 '08 #7

Markus
Expert 5K+
P: 6,050
Nice! I tried to think of a loop way but still very inexperienced when it comes to that. That's why I stick around here, always learning!
Indeed, indeed. It's a wonderful place!

Atleast now you know the logic for making a loop in this circumstance!
Feb 20 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.