Peb Mpeeb
Peb Mpeeb

Reputation: 77

How can I "POST" any data using table and loop (foreach)

I'm so confused with this problem.

As we can see from this code, I show the symptoms_data from database using loop. But, I want user can input a certainty value in each symptoms. The result of that code is only show a table with symptoms name and also the select box but, I don't know how can we save the value of each certainty symptoms because there's no id in each value.

      <tbody>
      <?php $i = 1;
      foreach ($symptoms_data->result() as $key) : ?>
        <tr>
          <td scope="col">
            <?= $i++; ?>
          </td>
          <td scope="col">
            <?php echo $key->symptoms_name ?>
          </td>
          <td scope="col">
            <select class="form-control" aria-label="Default select example" name="" id="" placeholder="" required>
              <option value=" " selected disabled>Choose Certainty Value</option>
              <option value="0.1 ">0.1</option>
              <option value="0.5 ">0.5</option>
              <option value="0.7 ">0.7</option>
              <option value="1 ">1</option>
            </select>
          </td>
          </td>
        </tr>
        <?php endforeach ?>
      </tbody>
    </table>
    <br>
  </div>
  <Button type="submit " class="btn btn-dark btn-block "> <i class="fa fa-check "></i> DIAGNOSE </button>
</form>

Upvotes: 0

Views: 375

Answers (1)

Delmontee
Delmontee

Reputation: 2364

You need to give the certainty element a named array value. So change the name="" part to something like name="certainty[]". If there is an id associated with each record (I assume there is), include that as a hidden value. eg. <input type="hidden" "name="id[]" value="12345" />.

Once form is submitted, in PHP cycle through each id element, using the index key as reference to the associated certainty value:

foreach($_POST['id'] as $key=>$value)
{
  $curr_record_id = $value;
  $curr_record_certainty = $_POST['certainty'][$key];
}

...then just store those details in the database table.

Upvotes: 1

Related Questions