Problems displaying data in modal with foreach

Asked

Viewed 68 times

-1

I am new in PHP and I am having a lot of difficulties in displaying the database information through a modal, I am accessing the database by a foreach, but the problem is that it only displays the modal with the last record.

excerpt from the code:

<div class="table-responsive">
      <table class="table table-striped table-sm">
        <thead>
          <tr>
            <th>#Id</th>
            <th>Nome | Paciente</th>
            <th>Data</th>
            <th>Exame</th>
            <th>Médico Solicitante</th>
            <th>Convênio</th>
          </tr>
        </thead>

  <?php foreach($dwl->findAll() as $value):?>

        <tbody>
          <tr>
            <td><?php echo $value->co_patientid; ?></td>
            <td><?php echo $value->na_patientname; ?></td>
            <td><?php echo $value->na_studydate; ?></td>
            <td><?php echo $value->na_description; ?></td>
            <td><?php echo $value->na_requestername; ?></td>
            <td><?php echo $value->na_insuranceplan; ?></td>
    <td>
     <button type="button" class="btn btn-danger">Excluir</button>
     <button type="button" class="btn btn-warning">Editar</button>
     <button type="button" class="btn btn-info" data-toggle="modal" data-target="#ModalView-<?php echo $value->co_patientid; ?>">Visualizar</button>
   </td>
  </tr>
  </tbody>

<?php endforeach ; ?>

 </table>
    </div>

<div class="modal fade" id="ModalView-<?php echo $value->co_patientid; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="modalWorkView"><td>Informações</td></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="table-responsive">

          <p><b>#Id:</b> <?php echo $value->co_patientid; ?> </p>
          <p><b>Paciente:</b> <?php echo $value->na_patientname; ?> </p>
          <p><b>Data do Exame:</b> <?php echo $value->na_studydate; ?> </p>
          <p><b>Descrição Exame:</b> <?php echo $value->na_description; ?> </p>
          <p><b>Médico Solivitante:</b> <?php echo $value->na_requestername; ?> </p>
          <p><b>Convênio:</b> <?php echo $value->na_insuranceplan; ?> </p>
          <p><b><hr></b></p>
          <p><b>Comentário</p></b>
          <p><?php echo $value->na_comment; ?> </p>

      </div>
    </div>
  </div>
</div>
</main>

1 answer

0

You can set the values in the modal with javascript as follows.

In the button that calls the modal stays that way.

   <button type="button" class="btn btn-info" data-toggle="modal" data- 
     target="#ModalView-<?php echo $value->co_patientid; ?> onclick - 
     "setModal(co_patientid)" ">Visualizar</button>

The java script this way.

  function setModal(co_patientid){
     document.getElementById('co_patientid').value = co_patientid;
}

And in the field of modal could create something similar

   <p id="co_patientid"></p> 

To make it work you need to download and import java script to your page/project.

Using Blade does not look very cool but can pass the modal into the foreach that display the results in the table.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.