Why is this not working? - extracting Boolean from MySQL to Checkbox

Discussion in 'PHP' started by abrown72, Aug 27, 2008.

  1. #1
    Hey all, (php newbie)

    I have read so many tutorials and everything says this should work but it won't. My check box will not check if the archived value is true.

    I have this value in my table 'archived' is a boolean and inserts fine from another form I have, when I open up the table and view the values of 'archived' I see either b'0' or b'1' obviously 1 is true or archived.

    Now on my edit form I am try to show a checkbox, checked if archived is true and not checked if archived is false.

    So at page load I am running my select statement and have this value assigned.

    $var = $row[archived];

    Then my code to generate my checkbox is like so.

    <?php
    if ($var == 1)
    {
    echo "<input type='checkbox' name='archive_ceck' value='1' checked />";
    }
    else
    {
    echo "<input type='checkbox' name='archive_ceck' value='1' />";
    }
    ?>

    I have tried many combinations of the if ($var) statement all with no luck, also if I echo the $var I get a little square box, I expected to see a 1 or 0.

    Any help would be great!
     
    abrown72, Aug 27, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Reading the var should be good, but try adding ' to the name

    ex: $var = $list['yourvar']; <-- see the '

    ok!? next you can do adding the checked part more easy

    ex: echo '<input type="checkbox" name="archive_ceck" value="1"' " . (($var == 1) ? 'checked="checked"' : "") . " />";

    that's it.. but is the data saved correctly? if not you don't get the result you like!
     
    EricBruggema, Aug 27, 2008 IP
  3. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Before running the following code, please check to make sure the datatype of the field is set to BOOLEAN ie: meaning it can be either set to true or false.

    
    <?php
    
    if(is_bool($var))
    {
    	if($var)
    	{
    		echo "<input type='checkbox' name='archive_ceck' value=1 checked=true/>"; 		
    	}
    	else
    	{
    		echo "<input type='checkbox' name='archive_ceck' />"; 		
    	}
    }
    else
    {
    	echo "The tested variable is not a BOOLEAN";
    }
    
    ?>
    
    PHP:
    That should work for you, if it says it's not a BOOLEAN then the database type is set incorrectly.

    Hope that helps,

    Regards,

    Steve
     
    Steve136, Aug 27, 2008 IP
  4. abrown72

    abrown72 Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks people, I looked through the DB. I am using MySQL query browser and when I create a column and tell it to create as a BOOLEAN for some reason it kept making it a TINYINT?...

    So I deleted the column, and created a new archive column and just made it an INT then I am just using

    if ($var == '1') and that solved all of my problems.

    Thanks for the heads up on checking to see if it was in fact a BOOLEAN value in the DB.
     
    abrown72, Aug 27, 2008 IP