简体   繁体   中英

How to make checkboxes on row checked/unchecked with values 1/0 from database?

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'"; } ?> <?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 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"];  
 $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" class="jmeno">'.$row["SerialNumber"].'</td>
  <td contenteditable="true">'.$row["Produkt"].'</td>
  <td contenteditable="true">'.$row["Oddělení"].'</td>
  <td contenteditable="true" class="jmeno">'.$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;
?>
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" class="jmeno">'.$row["SerialNumber"].'</td>
      <td contenteditable="true">'.$row["Produkt"].'</td>
      <td contenteditable="true">'.$row["Oddělení"].'</td>
      <td contenteditable="true" class="jmeno">'.$row["Místnost"].'</td>
      <td contenteditable="true">'.$row["IP"].'</td>
      <td contenteditable="true">'.$row["USB"].'</td>
      <td contenteditable="true">'.$row["Datum"].'</td>
     </tr>
     ';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM