PHP doubt how to generate a counter in the name of a radio button

0

I have a panel survey manager where I add, edit and delete dimensions and questions of the survey, through this the survey that contains 4 dimensions, in a total of 21 questions and with a likert scale, is applied.

My problem is based on placing a counter so that the name of each radio button is different I will attach an example:

  

The question 1 name="likert1" , the question 2 name="likert2" and so on   Question 21 name="likert 21" .

I enclose the code where I make the consultations

<?php
    require 'conexion.php';
     mysqli_query($mysqli,"SET NAMES 'utf8'");

  ?>



  <html lang="es">

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-theme.css" rel="stylesheet">
    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </head>




  <body>
    <div class="container">
      <div class="row">
        <h2 style="text-align:center">Encuesta estudiantes</h2>
        <br>
      </div>





      <?php
        $sql = "SELECT * FROM dimension";
        $resultado = $mysqli->query($sql);

        foreach($resultado as $row){


        ?>


        <div class="row table-responsive">
          <table class="table table-striped">
            <thead>
              <tr>
                <th>
                  <?php echo $row['nombre_dimension']; ?>
                </th>


                <th width="10%">No aplica</th>
                <th width="10%">Muy en desacuerdo</th>
                <th width="10%">En desacuerdo</th>
                <th width="10%">En acuerdo</th>
                <th width="10%">Muy en desacuerdo</th>
              </tr>
            </thead>
            <tbody>

              <?php   


              $sqlP = "SELECT * FROM preguntas WHERE id_dimension_p=".$row['id_dimension'];
              $resultadoP = $mysqli->query($sqlP) or die (mysqli_error());
                 foreach($resultadoP as $row){
              $likert=0;

              ?>


              <tr>
                <td>
                  <?php echo $row['nombre']; ?>
                </td>
                <td>
                  <center><input type="radio" name='<?php echo $likert; ?>' value="no aplica"></center>
                </td>
                <td>
                  <center><input type="radio" name='<?php echo $likert; ?>' value="muy en desacuerdo"></center>
                </td>
                <td>
                  <center><input type="radio" name='<?php echo $likert; ?>' value="en desacuerdo"></center>
                </td>
                <td>
                  <center><input type="radio" name='<?php echo $likert; ?>' value="en acuerdo"></center>
                </td>
                <td>
                  <center><input type="radio" name='<?php echo $likert; ?>' value="muy en desacuerdo"></center>
                </td>

              </tr>


              <?php }
                $likert++;
            }?>


            </tbody>

          </table>

        </div>

    </div>

  </body>

  </html>

I await your guidance or help, thank you very much

    
asked by alejandro henriquez 07.09.2017 в 22:21
source

2 answers

1

If you want all the radios to be called likert + number of the sequence, starting with 1:

  • You have to modify the value of the initial variable, setting it to 1:

    $likert=1;
    
  • You have to move it from place, placing it outside the loop for

    $likert=1;
    
    foreach($resultadoP as $row){
          // $likert=0; borrar esto
    
          //resto del código
    
  • Then, to each line you apply something like this:

    name='<?php echo "likert".$likert; ?>'
    

    Example, in the case of not applicable:

    <center><input type="radio" name='<?php echo "likert".$likert; ?>' value="no aplica"></center>
    

    And so with everyone else.

  • You have to move the increment of $likert to be inside the loop, that way it will increase in each iteration.

        $likert++;
    <?php }
    
  • answered by 08.09.2017 / 00:07
    source
    0

    Put your variable $ likert before the first for and start at 1

    $likert=1;
    foreach($resultado as $row){
    

    modify the line that defines the name in all its cases (does not apply, my disagreement etc):

    <center><input type="radio" name='<?php echo "likert".$likert; ?>'
    

    It's all.

        
    answered by 08.09.2017 в 20:52