Results 1 to 15 of 15

Thread: Select Menu issue

  1. #1

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Select Menu issue

    hai friends,

    my select Menu as follows

    Code:
    <select name="cmbgender" id="cmbgender" >
                    <option>Male</option>
                    <option>Female</option>
                </select>
    When the user attempt to edit a record, i load the data relavent to the record from the db in to a form where above control is a part of it.

    Now i need to select Male if the value in the db is Male, else Female.

    How do i do that ?

    Assume, $gender is the variable which holds the Male / Female data retrived.

  2. #2
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: Select Menu issue

    This query should work for most (some) DB servers.
    Code:
    "SELECT * FROM people WHERE gender = '" . $gender .  "'"
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  3. #3

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Select Menu issue

    brother k1II3dr3rd4gorOn

    Thanks. but my question is how to select an existing item in the list

  4. #4
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: Select Menu issue

    Quote Originally Posted by Fazi
    brother k1II3dr3rd4gorOn

    Thanks. but my question is how to select an existing item in the list
    Prehaps something like:

    Code:
    if ($gender == "male") {
    $menu = '<select name="cmbgender" id="cmbgender" >
                    <option value="male">Male</option>
                    <option value="female">Female</option>
                </select>';
    } else {
    $menu = '<select name="cmbgender" id="cmbgender" >
                    <option value="female">Female</option>
                    <option value="male">Male</option>
                </select>';
    }
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  5. #5

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Select Menu issue

    Hai K1,

    i just got a reply from an out side forum.
    Code:
    <option <?php if ($gender=="Male") { ?> selected="selected" <?php }?> >Male</option>


    I thing your method in #4 will have a large over head when it come to a big list

  6. #6
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Select Menu issue

    Yeah, the way he did it would be ok for 2 items, but for multiple it would be best to go through a loop.
    My usual boring signature: Something

  7. #7

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Select Menu issue

    Guys, Do you undestind this
    Code:
    <option <?php if ($gender=="Male") { ?> selected="selected" <?php }?> >Male</option>
    I have some confusion and explanation would be much appriciated.

  8. #8
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Select Menu issue

    well it is pretty straight forward...

    the php is saying:
    if the gender is male, then echo "selected='selected'"

    so you would need this:
    PHP Code:
    <select>
         //so if the person is male, this one will be selected
         <option value='male' <?PHP if($gener=="male") { ?> selected='selected <?PHP ?>>
         // if it is a female, this will be seleced
         <option value='female' <?PHP if($gener=="female") { ?> selected='selected <?PHP ?>>
    </select>
    My usual boring signature: Something

  9. #9

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Select Menu issue

    still i have confusing on the open and close tags,
    why this is imediatly colose?
    Code:
    <?PHP if($gener=="male") { ?>
    so what will happen to the following html?
    Last edited by Fazi; Oct 4th, 2007 at 07:36 PM.

  10. #10
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Select Menu issue

    well that is the correct way to show html with php.

    another (incorrect) way to show it would be:

    <?PHP if ($gener=="male") { echo "selected='selected'" } ?>
    My usual boring signature: Something

  11. #11

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Select Menu issue

    Thanks dclamp for the info.

  12. #12
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Select Menu issue

    no problem If your problem is solved, set it as resolved and give rep accordingly
    My usual boring signature: Something

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Select Menu issue

    It's <?php, not <?PHP.
    Also, you need to specify the value attribute on each option.

    An alternative way of doing it:
    PHP Code:
    <?php
      $s 
    ' selected="selected"';
      
    $sel $gender == 'male' ? array($s'') : array(''$s);
    ?>
    <select name="cmbgender" id="cmbgender">
      <option value="male"<?php echo $sel[0?>>Male</option>
      <option value="male"<?php echo $sel[1?>>Female</option>
    </select>
    This is overkill in this particular case, but preferable if the comparison is complex or the list of options is long, since it is only evaluated once per set of options instead of once per option.
    Last edited by penagate; Oct 7th, 2007 at 02:41 AM.

  14. #14
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Select Menu issue

    @pena: why is <?PHP not a correct way to open php tags?
    My usual boring signature: Something

  15. #15
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: Select Menu issue

    Quote Originally Posted by dclamp
    @pena: why is <?PHP not a correct way to open php tags?
    http://www.php.net/manual/en/language.basic-syntax.php

    All the ways the show it should be php not PHP.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width