Skip to content
Advertisement

Radio button for each row data retrieve from database

<tbody>
    <?php
    $query = "SELECT sm01.SM01_01, sm01.SM01_03, sm13.SM13_02 = sm13.SM13_03 FROM "
    . "sm13 INNER JOIN sm01 on sm01.SM01_01 = sm13.SM13_03 WHERE sm13.SM13_02 = $id";
    $sql_run = mysqli_query($link, $query);
    $j = 0;
    $count = mysqli_num_rows($sql_run);
    if (mysqli_num_rows($sql_run) > 0) {
        while ($row = mysqli_fetch_array($sql_run)) {
    ?>
      <tr>
        <th scope="row"><?php echo $j = $j + 1; ?></th>
        <td class="text-left"><input type="hidden" class="form-control-plaintext" name="id_ahli[]" value="<?php echo $row['SM01_01']; ?>">
          <?php echo $row['SM01_03']; ?></td>
        <td><div class="form-check-inline">
            <input type="radio" checked="" name="status"id="radio1<?php echo $row['SM01_03']; ?>" class="form-check-input">
            <label class="form-check-label" for="radio1<?php echo $row['SM01_03']; ?>">Tidak</label>
          </div>
          <div class="form-check-inline">
            <input type="radio" name="status" id="radio2<?php echo $row['SM01_03']; ?>" class="form-check-input">
            <label class="form-check-label" for="radio2<?php echo $row['SM01_03']; ?>">Hadir</label>
          </div></td>
      </tr>
    <?php
        }
    }
    ?>
</tbody>

I success to retrieve the data but i got the problem with the radio button where I want each row the radio button is checked for the Tidak label

The image of the problem is here

Advertisement

Answer

In your current code name attribute of radio button is same so only one radio will be set as selected .Instead change your code like below :

<tr>
    //other html code  //added here name_rowno 
     <td><div class="form-check-inline">
            <input type="radio" checked="" name="status_<?php echo $row['SM01_03']; ?>"id="radio1<?php echo $row['SM01_03']; ?>" class="form-check-input">
            <label class="form-check-label" for="radio1<?php echo $row['SM01_03']; ?>">Tidak</label>
          </div>
          <div class="form-check-inline">
            <input type="radio" name="status_<?php echo $row['SM01_03']; ?>" id="radio2<?php echo $row['SM01_03']; ?>" class="form-check-input">
            <label class="form-check-label" for="radio2<?php echo $row['SM01_03']; ?>">Hadir</label>
          </div></td>
  </tr>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement