How to change the name of a radio automatically?

1

Good evening, I hope you can help me I have this while inside another while and it shows 5 for each row, so I need your name to change in each row, I do not know if I explain

<?php include("cabeceras/sesion.php"); ?>

<?php 
if(isset($_SESSION['rol'])) { 
if ($_SESSION['rol'] == "3") {

 ?>
 <div class="container well">
    <h2 class="text-center">Registro de notas</h2>
 </div>

   <form action="#" method="POST">
     <div class="container well">
 <table class="table table-bordered text-center">
        <tr>
            <td class="alert-danger">N°</td>
            <td class="alert-success">Indicadores</td>
            <?php 
            include_once("conexion/conexion.php");
            $con=conectar();
            $consulta="SELECT * FROM ponderaciones";
            $consultar=$con->query($consulta);
            while ($row1=$consultar->fetch_array(MYSQLI_BOTH)) {

              echo '<td class="alert-info">'.$row1['ponderacion'].'</td>';
            }
             ?>

        </tr>

        <?php 
        include_once("conexion/conexion.php");
        $con=conectar();
        $id_docente=$_SESSION['id_docente'];
        $count=1;
        $sql="SELECT * FROM indicadores";
        $sqlr=$con->query($sql);
        while ($row=$sqlr->fetch_array(MYSQLI_BOTH)) {

            ?>

            <tr>
                <td><?php echo $count; $count++; ?></td>
                <td class="text-left"><input name="id_indicador" type="hidden" value="<?php echo $row['id_indicador']; ?>"><?php echo $row['indicador']; ?></td>

                <?php 
                $consulta="SELECT * FROM ponderaciones";
                $consultar=$con->query($consulta);
                echo '<fieldset>';
                while ($row3=$consultar->fetch_array(MYSQLI_BOTH)) {
                echo '<legend>Ponderacion</legend>';
                 echo '<td><input name="id_ponderacion" if("'.$row3['id_ponderacion'].'"=="A") {echo "checked"} type="radio" value="'.$row3['id_ponderacion'].'"> </td>';
                }
                echo ' </fieldset>';
                 ?>

            </tr>
    <?php  
        }
         ?>

</table>

    </div>

    <div class="container well text-center">
            <input type="reset" class="btn btn-danger" value="Limpiar">
            <input type="submit" class="btn btn-success" value="Registrar Evaluación">
    </div>

      </form>

<?php include("cabeceras/pie.php"); ?>

<?php } else { ?>   
        <script type='text/javascript' language='javascript'>
        alert('USTED NO TIENE ACCESO A ESTA PARTE DE LA PAGINA')  
        document.location.href='index.php'   
        </script> 
<?php } } else { ?>
        <script type='text/javascript' language='javascript'>
        alert('USTED NO TIENE ACCESO A ESTA PAGINA')  
        document.location.href='conexion/logout.php'     
        </script> 
<?php } ?>

<?php 

$id_alumno=$_GET['id_alumno'];
$id_indicador=$_POST['id_indicador'];
$id_inponderacion=$_POST['id_ponderacion'];

echo $id_inponderacion;
echo "<br>";
var_dump($_POST);
 ?>

    
asked by jorgnv 20.04.2017 в 05:30
source

2 answers

2

Instead of using a hidden field like this:

<input name="id_indicador" type="hidden" value="<?php echo $row['id_indicador']; ?>">

Which, repeat the name and it will not help you much when sending the form since it does not tell you which group of radio is associated with. You can use the id_indicador as a subfix of the radio button. Be careful, this is an approach between several different possible:

Where you have the loop of ponderaciones , change the name of <input> .

<input name="resultado[<?php echo $row['id_indicador']; ?>]" 

With this structure in your $_POST you will receive an array like this:

array(2) { ["resultado"]=> 
                  array(3) { 
                     [1]=> string(1) "1" 
                     [2]=> string(1) "3" 
                     [3]=> string(1) "3" 
                  } 
           ["submit"]=> string(20) "Registrar Evaluación" 
}

The trick of this is that php convert elements with this syntax into their name in arrays auto-magically. I mean when we have elements with names name="resultado[1]" , name="resultado[2]" .. etc. So it is a great help.

Do not worry if the ids are skipped or disorganized, they will arrive correctly. Just make sure to use numeric ids.

Then you just have to loope about it:

foreach ($_POST['resultado'] as $id_indicador => $id_ponderacion) {
    // para cada $id_indicador el $id_ponderacion seleccionado
}

I hope it serves you.

    
answered by 20.04.2017 / 06:25
source
0

try to create a counter which auto-matches you and concatenate it to the name with that you will have an autonumeric identifier when picking up with a for the number of elements and you can insert them in BD one by one.

$contador = 0;
while ($row3=$consultar->fetch_array(MYSQLI_BOTH)) {
                echo '<legend>Ponderacion</legend>';
                 echo '<td><input name="id_ponderacion" if("'.$row3['id_ponderacion'].'"=="A") {echo "checked"} type="radio" value="'.$row3['id_ponderacion'].$contador'"> </td>';
$contador++;
                }
    
answered by 20.04.2017 в 13:26