Problem displaying information with a foreach inside another foreach

0

Good evening. I need to display information with one foreach inside another. I will try to explain, to see if it facilitates. I get a list of exams from DB. This list is displayed in a table, as follows, with the use of a foreach:

Name --- Date --- Price
Item1 ----- X1 ------ Y1 ------ EDIT ------ CANCEL
Item2 ----- X2 ------ Y2 ------ EDIT ------ CANCEL

According to the code:

<?php
        foreach ($lista as $linha):
      ?>
     <tr class="destaque">
        <td class="destaque"><?= $linha['name'] ?></td>
        <td class="destaque"><?= $linha['date'] ?></td>
        <td class="destaque"><?= $linha['price'] ?></td>
        <td> 
          <div class="table-responsive">
            <form method="post" action="router.php?op=12">  
              <input type="hidden" id="idTestRemove" name="idTestRemove" value="<?= $linha['id'];?>">
              <a id="abuttontomodal" class="btn btn-warning btn-xs" href="#" data-toggle="modal" data-target='#<?= $linha['id']?>'>Editar</a>
              <button type="submit" name="delete" class="btn btn-danger btn-xs">Cancelar Exame</button>
            </form>
          </div>
        </td>
    </tr>

The CANCEL button removes that exam from the DB, while the EDIT button opens a modal for editing. The problem is that when opening the modal, I need a select to select a name, given a list retrieved also from the DB. I use a foreach, however the select is only displayed the first time, that is, when I click the edit button of Item 1 . If I click on Item 2 , for example, the select options are not populated.

<table align="center">
      <thead>
      <tr class="destaque">
        <th class="destaque">Procedimento</th>
        <th class="destaque">Data</th>
        <th class="destaque">Preço</th>
        <th class="destaque">Ações</th>
      </tr>
      </thead>
      <tbody>
      <?php
        foreach ($lista as $linha):
      ?>
     <tr class="destaque">
        <td class="destaque"><?= $linha['name'] ?></td>
        <td class="destaque"><?= $linha['date'] ?></td>
        <td class="destaque"><?= $linha['price'] ?></td>
        <td> 
          <div class="table-responsive">
            <form method="post" action="router.php?op=12">  
              <input type="hidden" id="idTestRemove" name="idTestRemove" value="<?= $linha['id'];?>">
              <a id="abuttontomodal" class="btn btn-warning btn-xs" href="#" data-toggle="modal" data-target='#<?= $linha['id']?>'>Editar</a>
              <button type="submit" name="delete" class="btn btn-danger btn-xs">Cancelar Exame</button>
            </form>
          </div>
        </td>
           <!-- Modal -->
    <div id="<?= $linha['id']?>" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-body">
                    <form action="router.php?op=13" method="post">
                <div class="form-group">  
                  <input type="hidden" name="idTest" id="idTest" class="form-text" value="<?=$linha['id']?>">
                   <select class="form-control input-sm" name="procedureId" id="procedureId">
            <option value="<?=$linha['procedure_id']?>"><?=$linha['name']?></option>
            <?php foreach ($lista3 as $e): ?>
               <option value="<?= $e['id']?>"><?=$e['name']?> </option>
            <?php endforeach ?>
            <input type="date" name="data" value="<?=$linha['date']?>">
                <button type="submit" name="edit" class="btn btn-success btn-xs">Editar</button>
                <button type="button" class="btn btn-danger btn-xs" data-dismiss="modal">Cancelar</button>
              </div>
            </form>
                </div>
            </div>
        </div>
    </div>
      <?php endforeach ?>
     </tr>
      <tr>
        <td class="destaque" colspan="2"><strong>TOTAL</strong></td>
        <td class="destaque"><strong><?= $lista2->fetchColumn(0) ?></strong></td>
      </tr>
    </tbody>
    </table>

Note: The modal is also within the first foreach.

    
asked by anonymous 22.05.2018 / 00:53

0 answers