skip to Main Content

I would like to check a checkbox if I have integer 1 stored in row a or b.

For now I created a working version to check for row ‘a’.
How can I check also for row ‘b’? So as said above if row a or/and b has integer 1 stored check the checkbox.

<input type="checkbox" name="a" id="a" value="1" <?php if($row['a'] == "1") { echo "checked"; }?>>

2

Answers


  1. This should work for you. this will echo checked to make the checkbox checked if any of the row contains integer 1

    <?php ($row['a'] == 1 || $row['b'] == 1) ? 'checked':''; ?>
    
    Login or Signup to reply.
  2. $checked = array('','checked');
    
    $row['a'] = 1;
    $row['b'] = 0;
    
    echo 'Row A<input type="checkbox" name="a" id="a" value="1"' . $checked[$row['a']] . ' /><br>';
    echo 'Row B<input type="checkbox" name="b" id="b" value="1"' . $checked[$row['b']] . ' /><br>';
    echo 'Row A or B<input type="checkbox" name="ab" id="ab" value="1"' . $checked[$row['a'] | $row['b']]   . ' />';
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search