Hi all, I am trying to insert a list box with a drop-down menu starting from 20 & counting up to 90.What I want to do is that I want to make this code such that it should display say, 50, by default.What should I add to this code to do that? <select name='price'> <?php for($i=20;$i<=90;$i++) { ?> <option value="<?php echo $i ?> "> <?php echo $i; ?> </option> <?php }?> </select> Code (markup): Thanks in advance.
<?PHP $defaultOption = 50; $optionBox = "<select name='price'>\n"; for($i=20;$i<=90;$i++) { if ($i == $defaultOption) { $optionBox .= "<option value='$i' selected='selected'>$i</option>\n"; } else { $optionBox .= "<option value='$i'>$i</option>\n"; } } $optionBox .= "</select>\n"; ?> <form name="test" action=""> <?PHP echo $optionBox ?> </form> PHP:
Thanks.But you have created the optionBox variable that was not in the code. I wanted to achieve the same objective by just adding to the existing code without altering it much. Is it not possible? Thanks in advance.
Add: <?php if ($i==50) echo ' selected="selected"'; ?> inside the <option> tag <select name='price'> <?php for($i=20;$i<=90;$i++) { ?> <option value="<?php echo $i ?>"<?php if ($i==50) echo ' selected="selected"'; ?>> <?php echo $i; ?> </option> <?php }?> </select> PHP:
Thanks everyone. But what does this code do?- $optionBox .= "</select>\n"; Code (markup): I can't understand what this "."(dot) before the = sign do? Also, \n means newline? And,optionBox is a variable or a keyword? Sorry to ask this questions but actually I am quite novice in programming. Thanks in advance.
.= is a shortcut for appending. e.g. $var .= 'string' is short for $var = $var . 'string' And yes, \n is a new line and $optionBox is a variable (in PHP, variables have a $ prefix)