Jump to content

Capturar Datos Desde Una Grilla Dinamica


Recommended Posts

Muchachos, les pido ayuda para resolver este problema, he probado de todo, pero talves aun lo le encuentro la logica a mi grilla dinamica (asi le digo yo), lo que quiero es capturar esos datos (intente con un array) y luego esos datos guardarlos en la base de datos, este es el extracto del scrip de mi programa, por favor ayudenme a encontrarle una solucion, saludos!!!

 

<?php  if($_POST[mostrar]){

$sql6 = "select * from materia where Id = '".$_POST[materia]."'";
$resultado2 = mysql_query ($sql6);
$row2 = mysql_fetch_array($resultado2);

echo $row2['Descripcion'];

$resultado=mysql_query($sql5= "SELECT
alumno.Rut,
alumno.Nombre as nombrea,
alumno.Apellido as apellidoa,
alumno.N_lista as nlista,
alumno.Curso,
notas.Id as Id,
notas.Rut_alumno,
notas.Id_materia,
notas.Semestre,
notas.Ano,
notas.nota1 as nota1,
notas.nota2 as nota2,
notas.nota3 as nota3,
notas.nota4 as nota4,
notas.nota5 as nota5,
notas.nota6 as nota6,
notas.nota7 as nota7,
notas.nota8 as nota8,
notas.nota9 as nota9,
notas.nota10 as nota10
FROM
alumno
Inner Join notas ON notas.Rut_alumno = alumno.Rut
WHERE
alumno.Curso =  '".$_POST[curso]."' AND
notas.Semestre =  '".$semestre."' AND
notas.Ano =  '".$ano."' AND
notas.Id_materia =  '".$_POST[materia]."' ORDER BY alumno.N_lista");

?>

<table border="1" cellspacing="0">
<tr>
<td width="20">Nº </td>
<td width="200">Apellidos</td>
<td width="200">Nombres</td>
<td width="47">Nota 1</td>
<td width="47">Nota 2</td>
<td width="47">Nota 3</td>
<td width="47">Nota 4</td>
<td width="47">Nota 5</td>
<td width="47">Nota 6</td>
<td width="47">Nota 7</td>
<td width="47">Nota 8</td>
<td width="47">Nota 9</td>
<td width="55">Nota 10</td>
</tr>

<?php
while($row = mysql_fetch_array($resultado)) {
  printf("<tr><td>".$row["nlista"]."</td><td>".$row["apellidoa"]."</td><td>".$row["nombrea"]."</td><td><input type='text' name='nota1".$row["Id"]."' id='nota1".$row["Id"]."' value='".$row["nota1"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota2".$row["Id"]."' id='nota2".$row["Id"]."' value='".$row["nota2"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota3".$row["Id"]."' id='nota3".$row["Id"]."' value='".$row["nota3"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota4".$row["Id"]."' id='nota4".$row["Id"]."' value='".$row["nota4"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota5".$row["Id"]."' id='nota5".$row["Id"]."' value='".$row["nota5"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota6".$row["Id"]."' id='nota6".$row["Id"]."' value='".$row["nota6"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota7".$row["Id"]."' id='nota7".$row["Id"]."' value='".$row["nota7"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota8".$row["Id"]."' id='nota8".$row["Id"]."' value='".$row["nota8"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota9".$row["Id"]."' id='nota9".$row["Id"]."' value='".$row["nota9"]."' size='3' maxlength='3'/></td><td><input type='text' name='nota10".$row["Id"]."' id='nota10".$row["Id"]."' value='".$row["nota10"]."' size='3' maxlength='3'/></td></tr>");
  }

   ?>

 </table>
<tr>
 <td> </td>
 <td> </td>
 <td> </td>
 <td><input type="submit" name="grabar" id="grabar" value="Grabar Cambios" />
 <?php  } ?> <!--aqui termina la condicion if post[mostrar] -->
 </td>
</tr>
</table>

<!--desde aqui grabaria los datos -->

<?php
if($_POST[grabar]){

$resultado=mysql_query($sql5= "SELECT
alumno.Rut,
alumno.Nombre as nombrea,
alumno.Apellido as apellidoa,
alumno.N_lista as nlista,
alumno.Curso,
notas.Id as Id,
notas.Rut_alumno,
notas.Id_materia,
notas.Semestre,
notas.Ano,
notas.nota1 as nota1,
notas.nota2 as nota2,
notas.nota3 as nota3,
notas.nota4 as nota4,
notas.nota5 as nota5,
notas.nota6 as nota6,
notas.nota7 as nota7,
notas.nota8 as nota8,
notas.nota9 as nota9,
notas.nota10 as nota10
FROM
alumno
Inner Join notas ON notas.Rut_alumno = alumno.Rut
WHERE
alumno.Curso =  '".$_POST[curso]."' AND
notas.Semestre =  '".$semestre."' AND
notas.Ano =  '".$ano."' AND
notas.Id_materia =  '".$_POST[materia]."' ORDER BY alumno.N_lista");

$datos=array();
   $fila=0;
   while($row= mysql_fetch_assoc($resultado)){
  $datos[$fila]['nota1']= $_POST['nota1'.$row["Id"]];
  $datos[$fila]['nota2']= $_POST['nota2'.$row["Id"]];
  $datos[$fila]['nota3']= $_POST['nota3'.$row["Id"]];
  $datos[$fila]['nota4']= $_POST['nota4'.$row["Id"]];
  $datos[$fila]['nota5']= $_POST['nota5'.$row["Id"]];
  $datos[$fila]['nota6']= $_POST['nota6'.$row["Id"]];
  $datos[$fila]['nota7']= $_POST['nota7'.$row["Id"]];
  $datos[$fila]['nota8']= $_POST['nota8'.$row["Id"]];
  $datos[$fila]['nota9']= $_POST['nota9'.$row["Id"]];
  $datos[$fila]['nota10']= $_POST['nota10'.$row["Id"]];

  $fila++;
  }

foreach($datos as $valor) {

$sql9= "INSERT INTO notas (nota1, nota2, nota3, nota4, nota5, nota6, nota7, nota8, nota9, nota10)
VALUES (".$valor['nota1'].",".$valor['nota2'].",".$valor['nota3'].",".$valor['nota4'].",".$valor['nota5'].",".$valor['nota6'].",".$valor['nota7'].",".$valor['nota8'].",".$valor['nota9'].",".$valor['nota10'].")";

$ingreso = mysql_query ($sql9);

} 
    } ?>
</form>
</body>
</html>

Edited by sefirotxx
Link to comment
Share on other sites

Tienes que en vez de mostrar los resultados de manera estática(como lo haces), mostrarlos, pero dentro de unas input type=text, ejemplo:

<?php
while($row = mysql_fetch_array($resultado)) {
printf("<tr><td><input type='text' name='numlista' value='<?php echo $row["nlista"]; ?>' /></td><td>");
}
?>

 

 

De esa forma podrás cambiar el valor de una nota que supongo, es lo que tu quieres hacer, de la forma que te digo tienes que hacer lo mismo con todos los campos notas(los que desees que se puedan modificar), y despues recibirlos al hacer submit al boton Grabar.

 

Cualquier duda seguire el tema.

 

Saludos :notonto:

Link to comment
Share on other sites

Gracias por responder. He intentado lo que me sugeriste

 

<?php
while($row = mysql_fetch_array($resultado)) { ?>
  <tr><td><?php echo $row["nlista"]; ?></td><td><?php echo $row["apellidoa"]; ?></td><td><?php echo $row["nombrea"];?></td><td><input type='text' name='nota1<?php echo $row["Id"]; ?>' id='nota1<?php echo $row["Id"]; ?>' value='<?php echo $row["nota1"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota2<?php echo $row["Id"]; ?>' id='nota2<?php echo $row["Id"]; ?>' value='<?php echo $row["nota2"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota3<?php echo $row["Id"]; ?>' id='nota3<?php echo $row["Id"]; ?>' value='<?php echo $row["nota3"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota4<?php echo $row["Id"]; ?>' id='nota4<?php echo $row["Id"]; ?>' value='<?php echo $row["nota4"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota5<?php echo $row["Id"]; ?>' id='nota5<?php echo $row["Id"]; ?>' value='<?php echo $row["nota5"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota6<?php echo $row["Id"]; ?>' id='nota6<?php echo $row["Id"]; ?>' value='<?php echo $row["nota6"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota7<?php echo $row["Id"]; ?>' id='nota7<?php echo $row["Id"]; ?>' value='<?php echo $row["nota7"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota8<?php echo $row["Id"]; ?>' id='nota8<?php echo $row["Id"]; ?>' value='<?php echo $row["nota8"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota9<?php echo $row["Id"]; ?>' id='nota9<?php echo $row["Id"]; ?>' value='<?php echo $row["nota9"];?>' size='3' maxlength='3'/></td><td><input type='text' name='nota10<?php echo $row["Id"]; ?>' id='nota10<?php echo $row["Id"]; ?>' value='<?php echo $row["nota10"];?>' size='3' maxlength='3'/></td></tr>

  <?php } ?>

 

pero sigo con el mismo resultado, te cuento lo que quiero hacer:

 

Este codigo es un pequeño fragmento de un sistema de notas que estoy desarrollando para web y justamente lo que quiero aca es mostrar las notas de la base de datos y tambien poder agregar, editar o borrar cuando apreto el boton guardar, solamente con cambiar el valor del input.

 

O sea esta es mi logica : si el input no tiene nada le pongo un dato e inserto (eso seria guardar), si el input tiene un dato por que lo capturo de la base de datos, puedo borrar o cambiar su valor y luego inserto ( editar y borrar sin hacer otra consulta mas que el mismo insertar)

 

Para encontrar el error he recorrido todo mi programa he encontrado el error exacto pero no se como solucionarlo:

 

El $_POST[materia] que viene desde mas atras lo reconoce la primera consulta cuando se apreta el submit mostrar... cuando apreto mostrar no hay problema, me imprime el $_POST[materia]... pero el problema es cuando apreto el siguiente submit guardar, es como si perdiera el dato contenido en $_POST[materia], por lo cual no m e funciona la consulta que hago en este submit, y obviamente mucho menos me va resultar lo del array ni lo del agregar...

 

Bueno intente almacenar $_POST[materia] en una variable por todos los lados de mi archivo donde si tiene el valor de la forma $idmateria = $_POST[materia], pero no hay caso, cuando apreto submit guardar pierdo el valor, y es raro por que no pierdo $_POST[curso] ni $semestre ni $ano como debiera ser... talves algo se me esta escapando

 

Por favor cualquier ayuda es bienvenida, gracias!!

 

------------------------------------

 

Solucionado!!! hice uso de $_GET para pasar los parametros y funciona sin ningun problema... se puede cerrar este post, muchas gracias!!

Edited by sefirotxx
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...