displaying a particular value as selected in dropdownlist

Discussion in 'PHP' started by learnerabn, Oct 12, 2010.

  1. #1
    Hi all,

    I want to display a particular number as selected in the dropdown list only if it is equal to $package->quantity I am having the following code but in the below code i'm having a problem.That problem is the particular number from that list is missing instead of selecting displaying as selected what may be the problem here can any body find it? the have gone through out the code 100 times but cant trace the mistake here.... the code is...
    <select>
    <?php
    for($i=1;$i<=10;$i++){
    echo "<option";
    if($i == $package->quantity)
    {
    echo "Selected='selected'";
    }
    echo ">";
    print $i;
    echo "</option>";
    }
    ?>
    </select>

    Thanks in advance...
     
    learnerabn, Oct 12, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    also initiate else.
     
    bartolay13, Oct 12, 2010 IP
  3. learnerabn

    learnerabn Peon

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    would it be a problem here?
    I dont think so...
    If yes means can you explain the reason?
    Any way i'll try it and reply you...
     
    learnerabn, Oct 12, 2010 IP
  4. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #4
    Add a space before "Selected". Probably not showing up because that particular option gets formatted as "<optionSelected='selected'>$i</option>".
    echo " Selected='selected'";
    PHP:
     
    Thorlax402, Oct 12, 2010 IP
  5. learnerabn

    learnerabn Peon

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes thanks a lot thorlax it works...
     
    learnerabn, Oct 13, 2010 IP