+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: php if argument

  1. #1
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    php if argument

    hey again guys,

    i am having a new problem. i am still working on creating a member profile feature for my website but i am having a problem with an if argument. when the person goes to the edit profile page i want all the information that is already in the profile to show up and be edited. i have the gender options set in a dropdown box and i want the current gender to be already selected on the edit profile page. so i figured an easy way would be a simple if statement saying if $'gender = M then the selected="selected" would be echoed for that options in the drop down box. the problem is there are three options (M,F,do not share) and all three of them are passing the if argument. if i change it to $gender==M then none of them pass. also i doubled checked to make sure that $gender was being properly set. i also tried it with multiple quotation/equal sign set ups. here is the code:

    Code:
    <?php $Gender = $ProfileContent[20]; ?>
        <label>Gender: </label><select name="Gender">
            <option <?php if($Gender = ""){ echo "selected=\"selected\""; } ?> value="">Do Not Share</option>
            <option <?php if($Gender = M){ echo "selected=\"selected\""; } ?> value="M">M</option>
            <option <?php if($Gender = F){ echo "selected=\"selected\""; } ?> value="F">F</option>
        </select>

  2. #2
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: php if argument

    Quote Originally Posted by garrensilverwing View Post
    if $'gender = M then the selected="selected" would be echoed for that options in the drop down box. the problem is there are three options (M,F,do not share) and all three of them are passing the if argument. if i change it to $gender==M then none of them pass. also i doubled checked to make sure that $gender was being properly set. i also tried it with multiple quotation/equal sign set ups.
    When using an if expression, dual equals must be used. If you only use one (as in $gender = M ) then what will happen is that PHP will assign M to that variable. Thus, it will always return true in addition to changing the variable.
    Additionally, all strings must be encapsulated by quotes. For this purpose, you can use either double or single quotes " or '. They are not however identical. Essentially, anything in single quotes will be output as you write it, whereas in double quotes it will be parsed by PHP before output (so for example, any variables will be output as their value in double quotes, where a single quote would output $myvariable).

    It is the combination of these factors which I think have been causing you issues. The code below should work for you.
    Oh, and whitespace doesn't matter. At all. Except in strings. $variable = 'value' is the same as $variable='value'

    PHP Code:
    <?php $Gender $ProfileContent[20]; ?>
        <label>Gender: </label><select name="Gender">
            <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
            <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
            <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
        </select>
    Last edited by Scoochi2; 09-23-2009 at 05:11 AM. Reason: typo fix
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  3. #3
    intertech2009 is offline x10Hosting Member intertech2009 is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    8

    Re: php if argument

    i have the same problem

  4. #4
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: php if argument

    well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement

  5. #5
    ah-blabla's Avatar
    ah-blabla is offline x10 Lieutenant ah-blabla is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    375

    Re: php if argument

    Quote Originally Posted by garrensilverwing View Post
    well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement
    The double == is the comparison operator for php, = assignment (as already said), so leave that as it is. (Single = definetely won't work). The problem is you shouldn't compare strings directly using $StringA == $StringB, rather you use a function for this:
    PHP Code:
    if (strcomp($StringA$StringB) == 0
    will return true if the two strings are the same.
    See: http://us.php.net/manual/en/function.strcmp.php

    Here's your code modified to work:
    PHP Code:
    <?php $Gender $ProfileContent[20]; ?>
        <label>Gender: </label><select name="Gender">
            <option <?php if(strcomp($Gender,"") == 0) { echo 'selected="selected"'; } ?> value="">Do Not Share</option>
            <option <?php if(strcomp($Gender,"M") == 0) { echo 'selected="selected"'; } ?> value="M">M</option>
            <option <?php if(strcomp($Gender,"F") == 0) { echo 'selected="selected"'; } ?> value="F">F</option>
        </select>

  6. #6
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: php if argument

    [FONT=Tahoma]
    Quote Originally Posted by garrensilverwing View Post
    well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement
    That's strange...

    As ah-blabla said, you can use a function to compare as well, but a direct string comparison DOES work.

    I executed the following code on my localhost.
    PHP Code:
    <?php $Gender 'M'?>
    <label>Gender: </label><select name="Gender">
        <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
        <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
        <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
    </select>
        
    <?php $Gender ''?>
    <label>Gender: </label><select name="Gender">
        <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
        <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
        <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
    </select>

    <?php $Gender 'F'?>
    <label>Gender: </label><select name="Gender">
        <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
        <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
        <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
    </select>
    The output was as follows:
    HTML Code:
     <label>Gender: </label><select name="Gender">
        <option  value="">Do Not Share</option>
        <option selected="selected" value="M">M</option>
        <option  value="F">F</option>
    </select>
        
    <label>Gender: </label><select name="Gender">
        <option selected="selected" value="">Do Not Share</option>
        <option  value="M">M</option>
        <option  value="F">F</option>
    </select>
    
    <label>Gender: </label><select name="Gender">
        <option  value="">Do Not Share</option>
        <option  value="M">M</option>
    So yes, what I said before does work. If it doesn't, I can only conclude there is an issue with the server and you should contact tech support.
    Last edited by Scoochi2; 09-23-2009 at 10:17 AM.
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: php if argument

    Quote Originally Posted by garrensilverwing View Post
    well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement
    Add a line of code:

    <?php $Gender = $ProfileContent[20]; ?>
    <?php echo "$Gender is " . $Gender . "<br \>" ?>
    <label>Gender: </label><select name="Gender">

    to show what $Gender is.
    Nothing is always absolutely so.

  8. #8
    ah-blabla's Avatar
    ah-blabla is offline x10 Lieutenant ah-blabla is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    375

    Re: php if argument

    The difference using strcomp() is that it is binary safe. (Whatever that means...) I'm just used to using methods to compare strings, since that's the only way in C and Java. The other thing I'm thinking is that the input variable isn't capitalised, i.e. you are comparing "m" to "M", which are two different things.

    Ps: I had a similar problem where comparing using == didn't work, but strcmp did. Also, you can use === for strict comparison, but that is unlikely to help here.
    Last edited by ah-blabla; 09-23-2009 at 01:28 PM.

  9. #9
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: php if argument

    <?php $Gender = $ProfileContent[20]; ?>
    <label>Gender: </label><select name="Gender">
    <option <?php if($Gender = ""){ echo "selected=\"selected\""; } ?> value="">Do Not Share</option>
    <option <?php if($Gender = M){ echo "selected=\"selected\""; } ?> value="M">M</option>
    <option <?php if($Gender = F){ echo "selected=\"selected\""; } ?> value="F">F</option>
    </select>

    If all you did was change '=' to '==' you are going to have another problem.

    Put M and F in quotes.
    Last edited by descalzo; 09-23-2009 at 01:12 PM.
    Nothing is always absolutely so.

  10. #10
    ah-blabla's Avatar
    ah-blabla is offline x10 Lieutenant ah-blabla is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    375

    Re: php if argument

    I was doing some more research and discovered:
    http://www.htmlforums.com/archive/in...p/t-25029.html
    The gist of this is you could have extra escape characters in your string, e.g. a new line etc, since the string you are checking is coming from somewhere external, i.e. an Array. Try trim($String) around the input string.

    BTW, are you sure you're getting the correct data from the array? In php I think array items are number from 0, so number 20 is the 21st element.
    Last edited by ah-blabla; 09-23-2009 at 01:36 PM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. Places to learn php
    By JaWasabi in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 01-13-2009, 02:03 AM
  4. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers