How to call names in php from a database?

0

Good afternoon friends I have this code in HTML (it's just part of the whole code) and even with a database, I would like to know how to say "Monica", "uzziel", "Andres" could bring the names that I have registered in my database.

A if I have my picture at the moment: link

My code.

 <table class="table table-bordered">
 <tr>
   <th>Docente</th>
   <th>1</th>
   <th>2</th>
   <th>3</th>
   <th>4</th>
   <th>5</th>
 </tr>

 <tr>
   <td>Monica</td>
   <td><input type="radio" name="respuestam"></td>
   <td><input type="radio" name="respuestam"></td>
   <td><input type="radio" name="respuestam"></td>
   <td><input type="radio" name="respuestam"></td>
   <td><input type="radio" name="respuestam"></td>
 </tr>   


      <tr>
   <td>Uzziel</td>
   <td><input type="radio" name="respuestau"></td>
   <td><input type="radio" name="respuestau"></td>
   <td><input type="radio" name="respuestau"></td>
   <td><input type="radio" name="respuestau"></td>
   <td><input type="radio" name="respuestau"></td>
 </tr> 


       <tr>
   <td>Andres</td>
   <td><input type="radio" name="respuestaA"></td>
   <td><input type="radio" name="respuestaA"></td>
   <td><input type="radio" name="respuestaA"></td>
   <td><input type="radio" name="respuestaA"></td>
   <td><input type="radio" name="respuestaA"></td>
 </tr> 

    
asked by uzziel sánchez díaz 08.08.2017 в 01:53
source

1 answer

1

This script can help you.

Create a table with these fields (for my test):

create table docente (
id INT NOT NULL AUTO_INCREMENT , 
nombre VARCHAR(30) NOT NULL , 
respuesta integer(1), 
PRIMARY KEY ('id'));

insert into docente values ( "", "Monica", 3),
  ("", "Uzziel",5), ("", "Andres", 4);

File php

   <?php
    // crear la conexion
    $mysqli = new mysqli("localhost", "user", "Password", "NombreDB");

/* verificar la conexión */
if ($mysqli->connect_errno) {
    printf("Conexión fallida: %s\n", $mysqli->connect_error);
    exit();
}

// consultar la tabla
$consulta = "SELECT * FROM docente";

?>


<table class="table table-bordered">
 <tr>
   <th>Docente</th>
   <th>1</th>
   <th>2</th>
   <th>3</th>
   <th>4</th>
   <th>5</th>
 </tr>
 
<?php
 // recorrer la tabla docente
 if ($resultado = $mysqli->query($consulta)) {
   while ($fila = $resultado->fetch_assoc()) {
   ?>
	 <tr>
	   <td> <?php echo $fila["nombre"] ?></td>
	   <?php for ( $i=1;$i<=5; $i++){
		   if( $i ==  $fila["respuesta"] ){ 
		            ?>
			  <td><input type="radio" 
		              name="<?php echo $fila["nombre"]; ?>"  
					  value="<?php echo $i; ?>"
					  checked ></td> 
		   <?php			  
		   }else{  
		   ?>
		   
		   <td><input type="radio" 
		              name="<?php echo $fila["nombre"]; ?>"  
					  value="<?php echo $i; ?>"></td>
	   <?php 
	       }
	   }
	   ?>
	 </tr>   

<?php

}
}
   ?>	
   </table>
    
answered by 08.08.2017 / 05:17
source