Why does this piece of code not work ?

Discussion in 'PHP' started by Sbhedges, Apr 13, 2010.

  1. #1
    Hello all,

    I'm having a small problem to do with checkboxes.

    Here is the code :
    
            <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>"
            <?php if($homelink = 1) { echo"checked"; } elseif($homelink !=1) { echo"unchecked"; } ?>/>
    
    Code (markup):
    $homelink can only be 1 or 0 (true or false) and yet when I view this code online the box is checked despite $homelink being set at 0 in the database.

    Any ideas ?

    Huge Thanks,
    Stephen
     
    Sbhedges, Apr 13, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You are doing assignment, not comparison....

    E.G if( $homelink = 1)

    You are assigning your variable to 1 not comparing the left and right hand side.......

    For comparison, you shuold use;

    if($homelink == 1)
    Code (markup):
    No. (1 or 0) and (true or false) are technically different things (though may give similar results.

    <?php
    $int = 1;
    $bool = true;
    
    if($int == 1) print "int is one\n";
    if($int === 1) print "int is int(one)\n";
    if($int == true) print "int is true\n";
    if($int === true) print "int is boolean true\n";
    
    if ($bool == 1) print "bool is one\n";
    if ($bool === 1) print "bool is int(one)\n";
    if ($bool == true) print "bool is true\n";
    if ($bool === true) print "bool is boolean true\n";
    ?>
    PHP:
    Output:
    int is one
    int is int(one)
    int is true
    bool is one
    bool is true
    bool is boolean true
    Code (markup):
     
    lukeg32, Apr 13, 2010 IP
  3. Sbhedges

    Sbhedges Peon

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Thanks for your detailed explanation, much appreciated.

    One issue now, I've changed the code to :
    
            <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>"
            <?php if($homelink == 1) { echo"checked"; } elseif($homelink !==1) { echo"unchecked"; } ?>/>
    
    Code (markup):
    However, I modify $homelink so it's $homelink=1 in the database
    But still the box remains unchecked,
    However when i changed $homelink to $homelink=0 in the database
    The box remains unchecked (what I want)

    So i'm baffled. Any thoughts ? It's probably me doing something wrong.
     
    Sbhedges, Apr 13, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Hi Stephen,

    No problem at all.


    You dont need the !== on the second comparison (only on 'equals' comparisons) so that is fine as !=

    I have tried your code here and it seems to be fine - I set the homelink value at the top of the code.

    <?php $homelink = 0; ?>
    PHP:
    Output: <input name="homelink" type="checkbox" id="homelink" value="" unchecked/>

    <?php $homelink = 1; ?>
    PHP:
    Output: <input name="homelink" type="checkbox" id="homelink" value="" checked/>

    After updating the value from the database, you are selecting it out somewhere above the code you pasted I assume - what is the value returned as ?
     
    lukeg32, Apr 13, 2010 IP
  5. mubheer@mvs.us

    mubheer@mvs.us Peon

    Messages:
    169
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    perhaps "==" for comparing ??

    <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>"
    <?php if($homelink == 1) { echo"checked"; } elseif($homelink !=1) { echo"unchecked"; } ?>/>

    actually you could try with a negative condition;

    <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>"
    <?php if($homelink != 0) { echo"checked"; } else { echo"unchecked"; } ?>/>
     
    mubheer@mvs.us, Apr 13, 2010 IP
  6. Sbhedges

    Sbhedges Peon

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Right, I've finally fixed this.

    Sadly the solution was alot easier then I expected.

    the code i posted was part of a function called dropper_admin_page();

    in that function i was calling another function containing all the variables, the function name was spelt wrong.

    Wow i feel embarrased now.....It's amazing how the simplist things are the answer.

    Although it won't have worked anyway without your help guys,

    Thank you so so much :)
     
    Sbhedges, Apr 13, 2010 IP