How to know the value of an array

0

I recently left a code about how to create dynamic ids in a while cycle.

Now I have this other doubt.

<?php
 $i=1;
 $n=1
 while($fila=$consulta->fetch(PDO::FETCH_ASSOC))
 {
    $this->empleados[]=$filas;
    $Filas = consult_cedula($fila['Persona']);

                 echo "<tr>";
                 echo '<td>' . $Filas['codsucursal'] . '</td>';
                 echo '<td>' . $fila['Persona'] . '</td>';
                 echo '<td>' . $fila['Fecha'] . '</td>';
                 echo '<td>' . $fila['Hora'] . '</td>';
                 echo '<td>' . $Filas['nombre'] . '</td>';
                 echo '<td>' . $Filas['cargo'] . '</td>';

                echo '<td>' . '<input type="button" name="nom' . $n .'" id="btn"' . $i .'"" class="btn btn-danger" value="..." data-fech="' .
                  $fila['Fecha'] . '" data-hora="' . $fila['Hora'] . '" data-ced="' . $fila['Persona'] .
                  '" data-nom="' . $Filas['nombre'] . '" data-cargo="' . $Filas['cargo'] . '">';

                 echo "</tr>";


                 $i=$i+1;
                 $n=$n+1;

             }

That's the code and as you can see, the id is already dynamic. Now, for each round the id is different, but how do I capture, for example, the second id with jQuery?
I'm allowing this query to only give me 5 results so I do not do it that long.

    
asked by Jdavid Barajas 08.05.2018 в 16:21
source

2 answers

0

According to what you need the code should be the following:

<?php
 $i=1;
 $n=1
 while(condicion)
 {
    // Suponiendo que $i es tu id y necesitas especificamente el id con valor 2
    if($i == 2){
       //Insersion en BD
    }

    $i=$i+1;
    $n=$n+1;

  }
?>

You tell us if it is what you need and it served you

    
answered by 08.05.2018 в 16:53
0

According to your information, it occurs to me in this way to obtain the id of the buttons within the table

$(document).ready(function(){
  
 $('#tabla').find('input').each(function() {
    console.log($(this).attr('id'));
    
  });
});
<script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>

<table border="1" id="tabla">
  <thead>
    <tr>
    <th>Columna1</th>
    <th>Columna2</th>
    <th>Columna3</th>
    <th>Columna4</th>
    <th>Columna5</th>
    <th>Columna6</th>
  </tr>

  </thead>
  <tbody>
    <tr>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <td>6</td> 
      <td><input type="button" value="Boton1" id="1"> </td>
    </tr>
    <tr>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <td>6</td> 
      <td><input type="button" value="Boton2" id="2"> </td>
    </tr>
   <tr>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <td>6</td> 
      <td><input type="button" value="Boton3" id="3"> </td>
    </tr>
    <tr>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <td>6</td> 
      <td><input type="button" value="Boton4" id="4"> </td>
    </tr>
  </tbody>
  
  </table>

What I do with that code is that I go through all the input in an orderly way and I get the value of the attribute id and I print it with console.log. Let me know if it helps you. Greetings!.

    
answered by 08.05.2018 в 17:52