Default selected

Discussion in 'PHP' started by 11ul, Sep 25, 2007.

  1. #1
    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.
     
    11ul, Sep 25, 2007 IP
  2. xemiterx

    xemiterx Peon

    Messages:
    62
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?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:
     
    xemiterx, Sep 25, 2007 IP
  3. 11ul

    11ul Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    11ul, Sep 25, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    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:
     
    krt, Sep 25, 2007 IP
  5. 11ul

    11ul Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
    :rolleyes:

    Thanks in advance.
     
    11ul, Sep 25, 2007 IP
  6. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #6
    .= 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)
     
    krt, Sep 25, 2007 IP