Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Help with shorthand IF conditional please

    I am trying to do a quick if conditional:

    Code:
    <select name="gender">
       <option value="male" <?php ($row[gender] == "male" ? "selected" : "") ?> >male</option>
       <option value="female" <?php ($row[gender] == "female" ? "selected" : "") ?> >female</option>
    </select>
    but it isn't working out.. when I select the users gender from the database and it is female it doesn't autoselect female

    **"male" and "female" are exact string matches to the db -- there are no trailing spaces...

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Try this:

    PHP Code:
    <select name="gender">
       <option value="male" <?php echo ($row['gender'] == "male") ? "selected" ""?> >male</option>
       <option value="female" <?php echo ($row['gender'] == "female") ? "selected" ""?> >female</option>
    </select>

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ah, *slap slap slap*
    Code:
    'selected="selected"'
    Sorry, thats the anal strict programmer in me
    Its called the Ternary (triple involvment) operator btw, just in case you need to look it up on the php website in the future.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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