简体   繁体   中英

How to retrieved data and store it in dynamic form according to the number of rows in php

I have a dynamic form wherein the user can add siblings in one form 在此输入图像描述

When the user click that button. The system will add another row of Name of Siblings , Date of Birth , Age and Education .

在此输入图像描述

Now, the information of Siblings are stored in my dabatabe and the name of the table is siblings

My question is how to retrieved that data and store it in dynamic form according to the number of rows This is my code:

$querySiblings = "SELECT * FROM siblings WHERE stud_inven_id = '$id'";
$resultSib = mysqli_query($connections, $querySiblings);
confirm_query($resultSib);

$check_row = mysqli_num_rows($resultSib);
if($check_row > 0){
  $fetch = mysqli_fetch_assoc($resultSib);

  //retrieve the dynamic form here...

}

And my dynamic form:

<div class="row">
  <div class="col-md-12">
    <h4>Name of Sibling/s according to birth order including you:</h4>
  </div>
</div>
<div class="row">
  <div class="col-md-4">
    <div class="form-group">
      <label>Name of Siblings</label>
      <input type="text" class="form-control" name="s_name[]" id="s_name">
    </div>
  </div>

  <div class="col-md-2">
    <div class="form-group">
      <label>Date of Birth<span id="check"></span></label>
      <input type="date" class="form-control" name="s_dob[]" id="s_dob" value="<?php echo date("Y-m-d"); ?>">
    </div>
  </div>
  <div class="col-md-1">
      <div class="form-group">
        <label>Age<span id="check"></span></label>
        <input type="text" class="form-control" name="s_age[]" id="s_age">
      </div>
  </div>
  <div class="col-md-4">
      <div class="form-group">
        <label>Education<span id="check"></span></label>
        <input type="text" class="form-control" name="s_educ[]" id="s_educ">
      </div>
  </div>
</div>
 <a href="#" id="addSiblings" class="btn btn-primary sib" >
  <i class="fa fa-fw fa-md fa-user-plus"></i>
</a>

I'm doing the UPDATE user information. Thank you in advance for helping me.

I think that the best solution is to use a while loop.

$querySiblings = "SELECT * FROM siblings WHERE stud_inven_id = '$id'";
$resultSib = mysqli_query($connections, $querySiblings);
confirm_query($resultSib);

while(($fetch = mysqli_fetch_assoc($resultSib)) != null){
  // Your form with $fetch['sib_id'], $fetch['stud_inven_id'], etc...
  echo <<<EOF

<div class="row">
  <div class="col-md-12">
    <h4>Name of Sibling/s according to birth order including you:</h4>
  </div>
</div>
<div class="row">
  <div class="col-md-4">
    <div class="form-group">
      <label>Name of Siblings</label>
      <input type="text" class="form-control" name="s_name[]" id="s_name" value="$fetch[name]">
    </div>
  </div>

  <div class="col-md-2">
    <div class="form-group">
      <label>Date of Birth<span id="check"></span></label>
      <input type="date" class="form-control" name="s_dob[]" id="s_dob" value="$fetch[dob]">
    </div>
  </div>
  <div class="col-md-1">
      <div class="form-group">
        <label>Age<span id="check"></span></label>
        <input type="text" class="form-control" name="s_age[]" id="s_age" value="$fetch[age]">
      </div>
  </div>
  <div class="col-md-4">
      <div class="form-group">
        <label>Education<span id="check"></span></label>
        <input type="text" class="form-control" name="s_educ[]" id="s_educ" value="$fetch[education]">
      </div>
  </div>
</div>
 <a href="#" id="addSiblings" class="btn btn-primary sib" >
  <i class="fa fa-fw fa-md fa-user-plus"></i>
</a>

EOF;
}

Be careful to don't add space or any other char before the last "EOF;"

I hope it was what you wanted.

I noticed from your text it said "according by birth order" so I added an extra ORDER BY to your query.

Here is what I came up with so far:

<?php
    $querySiblings = "SELECT * FROM siblings WHERE stud_inven_id = '$id' ORDER BY dob ASC";
    $resultSib = mysqli_query($connections, $querySiblings);
    confirm_query($resultSib);

    $check_row = mysqli_num_rows($resultSib);
?>

<div class="row">
    <div class="col-md-12">
        <h4>Name of Siblings according to birth order including you:</h4>
    </div>
</div>

<?php if ($check_row) { foreach (mysqli_fetch_all($resultSib, MYSQLI_ASSOC) as $results) { ?>

<div class="row">
    <div class="col-md-4">
        <div class="form-group">
            <label>Name of Sibling</label>
            <input type="text" class="form-control" value="<?= $results['name'] ?>" disabled>
        </div>
    </div>
    <div class="col-md-2">
        <div class="form-group">
            <label>Date of Birth<span id="check"></span></label>
            <input type="date" class="form-control" value="<?= $results['dob'] ?>" disabled>
        </div>
    </div>
    <div class="col-md-1">
        <div class="form-group">
            <label>Age<span id="check"></span></label>
            <input type="text" class="form-control" value="<?= $results['age'] ?>" disabled>
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group">
            <label>Education<span id="check"></span></label>
            <input type="text" class="form-control" value="<?= $results['education'] ?>" disabled>
      </div>
    </div>
</div>

<?php }} ?>

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