Execute cycle while several times once finished

0

Good afternoon, I know that my question is not very clear, but in this section I will explain myself better, it happens that I have a table that looks like this

but I want the radio input to be in all rows, this is my code:

echo "<table class='table table-hover'>
<tbody>";
while($alum = mysql_fetch_assoc($res2)){
echo "<tr>
    <th>".$alum['nombre_completo']."</th>";

    while($asis = mysql_fetch_row($res)){

    echo "<td> <input type='radio' name='asistencia' id='asistencia'           value='".$asis[0]."'><strong> ".$asis[1]."</strong></td>";
    }
echo "</tr>";
}
echo "</tbody>
</table>";

the while inside is only executed once and it is logical in my opinion, but I want to know how to do so that every time you start the first while the inner while is executed, as many times as the first while is executed, I hope I have expressed my best, thank you very much.

    
asked by alexi gallegos 18.12.2017 в 21:24
source

1 answer

0

Assuming that in each row you want the collection of 4 radio buttons, and your query $res gives four results:

$asis_buttons='';
while($asis = mysql_fetch_row($res)){
    $asis_buttons.= "<td><input type='radio' name='asistencia' id='asistencia' value='".$asis[0]."'><strong> ".$asis[1]."</strong></td>";
}

echo "<table class='table table-hover'><tbody>";
while($alum = mysql_fetch_assoc($res2)){
  echo "<tr><th>".$alum['nombre_completo']."</th>";
  echo $asis_buttons;
  echo "</tr>";
}
echo "</tbody></table>";

But even that solution is not going to work as expected. Your row of radio buttons has the same id for all elements, and will be in all rows. It would be better to make a template where you could replace the id and the student's name using sprintf :

<?php

$asistencias = [[1,'presente'],[2,'ausente'],[3,'desaparecido'],[4,'aparecido']];
$alumnos=['pedro','juan','diego'];
$asis_buttons='<tr><th>Alumno %1$s</th>';
foreach($asistencias as $asis){
    $asis_buttons.= '<td><input type="radio" name="asistencia%2$d" id="asistencia_'.$asis[0].'_%2$d" value="'.$asis[0].'"><strong>'.$asis[1].'</strong></td>';
}
$asis_buttons.='</tr>';


echo '<table>';
foreach($alumnos as $index=>$alumno) {
  echo sprintf($asis_buttons,$alumno,$index);
}
echo '</table>';
    
answered by 19.12.2017 в 14:08