PHP - Ayuda agenda php

 
Vista:
Imágen de perfil de Coajin

Ayuda agenda php

Publicado por Coajin (8 intervenciones) el 11/02/2017 23:12:18
Tengo unproblema con la tabla y es que no me muestra la informacion de el archivo (En el archivo la información se ordena así: "Nombre/Apellido/Telefono/Correo") ¿Como podría solucionarlo? Versión PHP: 7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE php>
<html lang="es">
<head>
	<meta charset="UTF-8">
	<title>Agenda</title>
	<link rel="stylesheet" type="text/css" href="css/estilos.css">
</head>
<body>
	<fieldset>
		<legend>Insertar Contacto</legend><br>
		<form action="" method="post">
			Nombre: <input type="text" name="nombretxt" /><br><br>
			Apellido: <input type="text" name="apptxt" /><br><br>
			Telefono: <input type="text" name="telefonotxt" /><br><br>
			Correo: <input type="text" name="correotxt" /><br><br>
			<input type="submit" value="Guardar Contacto" name="enviar">
		</form>
	</fieldset>
	<?php
		class Archivo
		{
			public $nombre;
			public $archivo;
 
			function __construct($n){
				$this->nombre = $n;
			}
 
			function insertar($nom,$app,$tel,$corr){
				if (file_exists($this->nombre)) {
					$this->archivo = fopen($this->nombre, "a");
					fwrite($this->archivo, PHP_EOL); //<br>
					fwrite($this->archivo, "$nom/$app/$tel/$corr");
					fclose($this->archivo);
				}else{
					$this->archivo = fopen($this->nombre, "w");
					fwrite($this->archivo, "$nom/$app/$tel/$corr");
					fclose($this->archivo);
				}
				echo "Contacto Insertado";
			}
 
			function consultar(){
				if(file_exists($this->nombre)){
					$this->archivo = fopen($this->nombre, "r+");
					?>
					<table>
						<tr>
							<td>Nombre</td>
							<td>Apellido</td>
							<td>Telefono</td>
							<td>Correo</td>
						</tr>
				<?php
				while(!feof($this->archivo)){
					$linea = fgetc($this->archivo);
					$arreglo = explode("/", $linea);
					if(count($arreglo)>=5) {
						echo "<tr>";
						echo "<td>$arreglo[0]</td>";
						echo "<td>$arreglo[1]</td>";
						echo "<td>$arreglo[2]</td>";
						echo "<td>$arreglo[3]</td>";
						echo "<td>$arreglo[4]</td>";
						echo "</tr>";
					}
				}
					echo "</table>";
					fclose($this->archivo);
				}else{
					echo "Agenda Vacía";
				}
			}
		}
		$obj = new Archivo("agenda.txt");
		if (isset($_POST["enviar"])) {
			$nom = $_POST["nombretxt"];
			$app = $_POST["apptxt"];
			$tel = $_POST["telefonotxt"];
			$corr = $_POST["correotxt"];
 
			$obj->insertar($nom,$app,$tel,$corr);
		}
		$obj->consultar();
 
	?>
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro
Val: 575
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Ayuda agenda php

Publicado por Alejandro (157 intervenciones) el 12/02/2017 18:02:14
Hola Coajin, debes cambiar la función fgetc(toma un caracter) por fgets(toma una linea del archivo) y tambien la condicion del if
ya que solo envias cuatro datos y en la condicion le dices que debe ser mayor igual que 5 por lo que nunca ejecutará el bloque del if.
Prueba cambiarlo por esto:

1
2
3
4
5
6
7
8
9
10
11
12
13
while(!feof($this->archivo)){
	$linea = fgets($this->archivo);
	$arreglo = explode("/", $linea);
 
      if(count($arreglo)>=4) {
	   echo "<tr>";
           echo "<td>$arreglo[0]</td>";
	   echo "<td>$arreglo[1]</td>";
	  echo "<td>$arreglo[2]</td>";
	  echo "<td>$arreglo[3]</td>";
          echo "<tr>";
      }
}

Comentanos si te sirvio. Un Saludo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
3
Comentar
Imágen de perfil de Coajin

Ayuda agenda php

Publicado por Coajin (8 intervenciones) el 12/02/2017 22:54:42
Funcionó perfecto, Gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar