Picking variable coming from foreach

Asked

Viewed 108 times

0

I’m breaking my head on a problem. I hope you help me please. I’m listing all the records in the database. I need to get the line id as the click goes. I’m only getting the first line ID, when I click on the next line the variable gives me the first record again.

*I took all the unnecessary css and Php out of the code to be quick and easy to understand :)

index

<table class="table table-hover">
                            <thead>
                                <tr>
                                    <th scope="col">Nº do Cliente</th>
                                    <th scope="col">Nome</th>

                                </tr>
                            </thead>
                            <tbody>

                                <?php

                                $Mostrar = new Crud();
                                foreach ($Mostrar->Listar() as $rs) {
                                    ?>


               <tr>
               <td><?php echo $rs['cli_id']; ?></td> <!--PRECISO DESSA VARIAVEL -->
                                        <td><?php echo $rs['cli_nome']; ?></td>

                                    </tr>
                                    <?php include 'includes/clientes/cadastrarMoto.php'; ?> <!--AQUI FICA O MODAL PRA APRESENTAR O ID -->


                                    <tr>
                                        <td colspan="6">
                                            <div class="pull-left">
                                                    <span class="material-icons adicionar" title="Motos" data-balao="tooltip"  data-toggle="modal" data-target="#cadastrarMoto">storage</span></div> <!--CLICANDO AQUI VAI PARA UM MODAL -->




                                        </td>
                                    </tr>


                                    <?php
                                }
                                ?>
                            </tbody>
                        </table>

register.php

<div class="modal fade">
.........
<?php echo $rs['cli_id']; ?>
  • How so line?

  • 1

    I voted for the closing of your question as unclear. I did not understand what you are wanting. Bottom line: what would be "line id"?

  • 1

    I advise you to rephrase the question because it is very confusing and if possible put the class 'Crud' so that we can test and find the best solution for your case, Casso n want to put the 'Crud' class although q explain the question better and explain what this Class would do

  • 1

    See if this link helps - http://www.davidchc.com.br/video-aula/videoaula-criando-carrinho-de-compra-com-php-orientado-a-objetos-parte-1/

  • Thank you for answering personally. I’m sorry if I was unclear. https://ibb.co/dpcGUo (SYSTEM IMAGE.) https://jsfiddle.net/njfLwx83/2/ (JA COM O METODO LISTAR() ) . I hope it’s a little clearer. thank you staff!

  • https://ibb.co/h5TRvT this is the modal screen. with <?php echo $rs['cli_id']; ? >. but it is fixed in COD 42. when I click on the SPAN of the COD 41 line, echo shows COD 42.

  • I tried to take the GET as parameter. even so it only shows the first cli_id in the foreach loop. =\

Show 2 more comments

1 answer

1

You can add in the "date" value of the link that opens the modal

<span class="material-icons adicionar" title="Motos" data-balao="tooltip"  data-toggle="modal" data-target="#cadastrarMoto" data-value-id="<?= $rs['cli_id']; ?>">storage</span>

and in the modal you take the value directly with javascript

Have examples here https://getbootstrap.com/docs/4.1/components/modal/#Varying-modal-content

  • staff I was able to resolve. The change was in the span and div of the modal. follows: https://jsfiddle.net/mk5o1zrw/ . THANK YOU! All comments were of paramount importance.

Browser other questions tagged

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