Tengo este código con el cual deseo mostrar unos datos de un JSON en una tabla, mi problema es que en la pantalla solo se imprimen las celdas de cabecera, y ahora que lo noto el link para volver tampoco se muestra.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="http://localhost/060522/eliminar.php" method="post">
<p>Para eliminar una materia ingresa la clave de la materia a eliminar y has click en eliminar</p>
Clave <input type="text" name="clave">
<input type="submit" value="Eliminar">
</form>
<table>
<tr><th>Clave</th><th>Semestre</th><th>Especialidad</th><th>Horas</th></tr>
<?php
$jsonfile="materias.json";
$materias=array();
$content=file_get_contents($jsonfile);
$materias=json_decode($content);
$def=0;
$varCount=count($materias);
while($def<=$varCount){
?>
<tr>
<td><?php echo $materias[$def]->key;?></td>
<td><?php echo $materias[$def]->semester;?></td>
<td><?php echo $materias[$def]->specialty;?></td>
<td><?php echo $materias[$def]->hours;?></td>
</tr>
<?php
$def++;
}
?>
</table>
<a href="/060522/menu.html">Volver</a>
</body>
</html>
Sin embargo, puse una fila y una celda justo como muestro abajo y esa si la imprimia.
$varCount=count($materias);
while($def<=$varCount){
?>
<tr>
<td>Hola a todos</td>
</tr>
<tr>
<td><?php echo $materias[$def]->key;?></td>
<td><?php echo $materias[$def]->semester;?></td>
<td><?php echo $materias[$def]->specialty;?></td>
<td><?php echo $materias[$def]->hours;?></td>
</tr>
<?php
$def++;
}
?>
</table>
<a href="/060522/menu.html">Volver</a>
Contenido de materias.json
{
"0":{"key":"9748026452","semester":"4","specialty":"Contabilidad","hours":"5"},
"1":{"key":"4376197623","semester":"4","specialty":"Mecanica","hours":"4"},
"2":{"key":"5637945638","semester":"4","specialty":"Contabilidad","hours":"5"},
"3":{"key":"4678415905","semester":"6","specialty":"Programacion","hours":"12"},
"4":{"key":"6130620013","semester":"6","specialty":"Mecanica","hours":"3"},
"5":{"key":"6130620013","semester":"6","specialty":"Mecanica","hours":"3"},
"6":{"key":"0907200134","semester":"3","specialty":"Laboratorista Clinico","hours":"4"},
"7":{"key":"0702200209","semester":"2","specialty":"Mecanica","hours":"4"},
"8":{"key":"4563856123","semester":"5","specialty":"Administracion","hours":"10"},
"9":{"key":"0861595377","semester":"2","specialty":"Administracion","hours":"7"},
"10":{"key":"8748638724","semester":"4","specialty":"Mecanica","hours":"6"},
"11":{"key":"563931287","semester":"4","specialty":"Administracion","hours":"9"},
"12":{"key":"6537821230","semester":"5","specialty":"Programacion","hours":"10"},
"14":{"key":"9823756738","semester":"4","specialty":"Mecanica","hours":"8"},
"15":{"key":"9230984792","semester":"1","specialty":"Administracion","hours":"6"},
"16":{"key":"0586573084","semester":"5","specialty":"Programacion","hours":"4"},
"17":{"key":"3102603106","semester":"3","specialty":"Contabilidad","hours":"7"},
"18":{"key":"8749386590","semester":"4","specialty":"Laboratorista Clinico","hours":"6"},
"19":{"key":"7484300027","semester":"5","specialty":"Contabilidad","hours":"12"}
}
Me he dado cuenta de algo mas, si mi archivo JSON esta de la siguiente manera LA TABLA SI PUEDE IMPRIMIRSE
[
{"key":"9748026452","semester":"4","specialty":"Contabilidad","hours":"5"},
{"key":"4376197623","semester":"4","specialty":"Mecanica","hours":"4"}
]
Como pudieron notar, arriba tengo un input text y un input submit, se supone que al ingresar la clave de la materia y dar en eliminar, se eliminará toda la información de dicha materia.
<?php
$clave=$_POST['clave'];
$jsonfile="materias.json";
$delete=array();
$content=file_get_contents($jsonfile);
$delete=json_decode($content);
$abc=0;
$varCount=count($delete);
while($abc<=$varCount){
if(($delete[$abc]->key)==$clave){
unset($delete[$abc]);
echo"all delete";
}
$abc++;
}
file_put_contents($jsonfile, json_encode($delete));
?>
Es después de dar click en eliminar que ocurre ese cambio en mi JSON.
¿Cómo podría resolverlo?
materias.json
?var_dump
a$materias[$def]
.$delete=json_decode($content, true);