1

I have the following table:

ID | Monday | Tuesday | Wednesday | Thursday | Friday
25 | 1      |  0      |   0       |  1       | 1

(this doesn't format right, imagine a table with zeros if a day is unchecked and ones if a day is checked)

And I want it on my html page to display like this:

Monday (checked checkbox)
Tuesday (unchecked checkbox)
Wednesday (unchecked checkbox)

and so on. I know that the input tag contains "checked" if you want the checkbox to be checked. but i need to insert an if condition into the input tag, that says if the $value is 1 then echo "checked". it is probably just a question of formatting the php inside the html inside the php correctly.

So far I have this PHP code, but it only gives me unchecked checkboxes without the day caption:

<html>    
<?php    
    $firstrow = false;
        while ($row = mysqli_fetch_assoc($res_skill)){
        if (!$firstrow) {
        foreach ($row as $column => $value) {
        echo "<input type='checkbox' name='data[]' value='". $column . "' /><br>"; 
        }
        $firstrow = true;
        }
        }
    ?>
</html>

What can I do?

5
  • Where did you expect to see the caption? Commented Dec 4, 2016 at 19:35
  • I haven't put the caption after the input tag yet, as I am not sure how to do it. The Caption is supposed to be the column name from my table. ETA: the caption appears when I put the $column after the input tag. The main question is, how do I display the box as checked if the $value is 1? Commented Dec 4, 2016 at 19:41
  • Column name is stored in $column isn't it? Then what is the problem? Commented Dec 4, 2016 at 19:42
  • the caption appears when I put the $column after the input tag. The main question is, how do I display the box as checked if the $value is 1? it is probably just a question of getting all the ""s and '' s right. Commented Dec 4, 2016 at 19:45
  • Use checked attrubite for input tag. Commented Dec 4, 2016 at 19:46

1 Answer 1

0

What you should be using is labels if you want to display captions for the checkboxes. value attribute is used while submitting a form to a page (in the form of name-value pairs). For keeping some day's checkbox ticked, you have to use checked attribute in checkbox element.

<?php    
   $firstrow = false;
   while ($row = mysqli_fetch_assoc($res_skill)){
     if (!$firstrow) {
        foreach ($row as $column => $value) {
           echo "<label for='".$column."ID'>".$column."</label>";
           echo "<input type='checkbox' name='data[]' id='".$column."ID' value='". $column . "'";
           if($value==1){
              echo " checked='checked'";
           }
           echo "/><br>"; 
        }
        $firstrow = true;
     }
   }
?>

Using id attribute in input element and for attribute in label element isn't relevant to your problem. But it is a good programming practice to do so. I didn't get why were you using $firstrow, so I've kept that part of the code unchanged.

In place of $value==1 you can use any other condition on which you want the checkbox to be checked by default.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't want to keep Monday checked by default, I want the Monday box to be checked if the value of the column for that specific ID is 1. For any other ID, it could be 0.
Here you go. I've added the changes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.