Home > Back-end >  How to make checkboxes on row checked/unchecked with values 1/0 from database?
How to make checkboxes on row checked/unchecked with values 1/0 from database?

Time:10-19

I have problems with my code. I have table filled with SQL data. I want to make my checkboxes checked if Barva = 1 and unchecked if Barva = 0.

I tried to add this code <?php if ($row['status'] == 1) { echo "checked='checked'"; } ?> between <td contenteditable="false"><form method="post"><input type="checkbox" name = checkbox'.$row[ID].'></form></td> but I failed with syntax.

Can someone help me please?

Index -this is how it looks like

SQL - sql view

$output = '
<br />
<table >
 <tr>
  <th width="1%">!</th>
  <th width="1%">ID</th>
  <th width="10%">Inventární číslo</th>
  <th width="10%">Serial Number</th>
  <th width="10%">Produkt</th>
  <th width="10%">Oddělení</th>
  <th width="10%">Místnost</th>
  <th width="10%">IP</th>
  <th width="10%">Síť/USB</th>
  <th width="10%">Datum</th>
  <th width="10%">Funkce</th>
 </tr>
';
while($row = mysqli_fetch_array($result))
{
 $page->checkbox_state = $row["Barva"];  
 $output .= '
 <tr>
  <td contenteditable="false"><form method="post"><input type="checkbox" 
  name = checkbox'.$row[ID].'></form></td>
  <td contenteditable="true">'.$row["ID"].'</td>
  <td contenteditable="true">'.$row["Inventárníčíslo"].'</td>
  <td contenteditable="true" >'.$row["SerialNumber"].'</td>
  <td contenteditable="true">'.$row["Produkt"].'</td>
  <td contenteditable="true">'.$row["Oddělení"].'</td>
  <td contenteditable="true" >'.$row["Místnost"].'</td>
  <td contenteditable="true">'.$row["IP"].'</td>
  <td contenteditable="true">'.$row["USB"].'</td>
  <td contenteditable="true">'.$row["Datum"].'</td>
 </tr>
 ';
}

$output .= '</table>';
echo $output;
?>

CodePudding user response:

I think you can use like  below to get checkbox checked:



 $output = '
    <br />
    <table class="table table-bordered table-striped">
     <tr>
      <th width="1%">!</th>
      <th width="1%">ID</th>
      <th width="10%">Inventární číslo</th>
      <th width="10%">Serial Number</th>
      <th width="10%">Produkt</th>
      <th width="10%">Oddělení</th>
      <th width="10%">Místnost</th>
      <th width="10%">IP</th>
      <th width="10%">Síť/USB</th>
      <th width="10%">Datum</th>
      <th width="10%">Funkce</th>
     </tr>
    ';
    while($row = mysqli_fetch_array($result))
    {
     $page->checkbox_state = $row["Barva"];  
     $isChecked="";
     if($row["Barva"]==1)
     {
      $isChecked="checked='checked'";
     }
     $output .= '
     <tr>
      <td contenteditable="false"><form method="post"><input type="checkbox" 
      name = checkbox'.$row["ID"].' '.$isChecked.'></form></td>
      <td contenteditable="true">'.$row["ID"].'</td>
      <td contenteditable="true">'.$row["Inventárníčíslo"].'</td>
      <td contenteditable="true" >'.$row["SerialNumber"].'</td>
      <td contenteditable="true">'.$row["Produkt"].'</td>
      <td contenteditable="true">'.$row["Oddělení"].'</td>
      <td contenteditable="true" >'.$row["Místnost"].'</td>
      <td contenteditable="true">'.$row["IP"].'</td>
      <td contenteditable="true">'.$row["USB"].'</td>
      <td contenteditable="true">'.$row["Datum"].'</td>
     </tr>
     ';
    }
  • Related