#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Add an item to dropdown list


    Hello, I have a while lopp generated dropdown list, and I would like to add "Select" at the top, so as soon as they see the list the first option is Select, not the items inside the list.

    Here is my dropdown menu:

    PHP Code:
    <select id="select2" name="select2">
    <?php
    $ids 
    0;
    while(
    $rows mysql_fetch_assoc($run)) {
        echo 
    "<option value=$ids>".$rows['Short']."</option>";
        
    $ids++;
    }
    ?>
    </select>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,939
    Rep Power
    1045
    Hi,

    so the first item should be selected? Isn't that the default behaviour?

    If it isn't, just check the counter:
    PHP Code:
    if ($ids == 0)
        ... 
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Yes it is selected, but I would like to add 1 predefined option in there called "select", and then continue with displaying other items..
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,939
    Rep Power
    1045
    Well, I don't know where this name "select" appears. But it works just like I said above: if the current element has that name, you add
    Code:
    selected="selected"
    to the option tag.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    You know...

    There are items inside my dropdown menu:

    Item 1
    Item 2
    Item 3
    Item 4
    ......


    And they are generated by the while loop:

    PHP Code:
    <select id="select2" name="select2">
    <?php
    $ids 
    0;
    while(
    $rows mysql_fetch_assoc($run)) {
        echo 
    "<option value=$ids>".$rows['Short']."</option>";
        
    $ids++;
    }
    ?>
    </select>
    Now as soon as user comes to the website they see Item 1 in dropdown menu. I want to add one more "item" called Select or Select item to this dropdown menu.. hope this is clear
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,939
    Rep Power
    1045
    Sorry, I misunderstood you.

    In this case simply echo that option above the loop:
    PHP Code:
    echo "<option value="0" selected="selected">Select</option>"
    (Make sure you don't accept this option as a valid selection.)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    No problem, you are helping me

    Thanks, this solved my problem

IMN logo majestic logo threadwatch logo seochat tools logo