Ver Mensaje Individual
  #6 (permalink)  
Antiguo 30/06/2010, 02:34
Cint
 
Fecha de Ingreso: junio-2010
Mensajes: 3
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: Agenda mediante una clase con array

Buenas!! gracias por las respuestas, ya he conseguido que me salga algo pero sin llegar a utilizar el bucle, ahora el problema que tengo es cómo pasar las propiedades del objeto persona que selecciono en el link.
Os dejo los distintos códigos que tengo en las páginas para que sepais como lo tengo.
Página persona:
Código PHP:
Ver original
  1. <body>
  2. <?php
  3. class Persona
  4. {
  5.     private $nombre;
  6.     private $apellidos;
  7.     private $telefono;
  8.     private $email;
  9.     private $direccion;
  10.     private $ciudad;
  11.     private $provincia;
  12.     private $pais;
  13.     private $codigoPostal;
  14.     private $fechaNacimiento;
  15.    
  16.     public function __construct($nombre, $apellidos, $telefono, $email, $direccion, $ciudad, $provincia, $pais, $codigoPostal, $fechaNacimiento)
  17.     {
  18.         $this->nombre = $nombre;
  19.         $this->apellidos = $apellidos;
  20.         $this->telefono = $telefono;
  21.         $this->email = $email;
  22.         $this->direccion = $direccion;
  23.         $this->ciudad = $ciudad;
  24.         $this->provincia = $provincia;
  25.         $this->pais = $pais;
  26.         $this->codigoPostal = $codigoPostal;
  27.         $this->fechaNacimiento = $fechaNacimiento;
  28.     }
  29.    
  30.     public function getNombre()
  31.     {
  32.         return $this->nombre;
  33.     }
  34.     public function getApellidos()
  35.     {
  36.         return $this->apellidos;
  37.     }
  38.     public function getTelefono()
  39.     {
  40.         return $this->telefono;
  41.     }
  42.     public function getEmail()
  43.     {
  44.         return $this->email;
  45.     }
  46.     public function getDireccion()
  47.     {
  48.         return $this->direccion;
  49.     }
  50.     public function getCiudad()
  51.     {
  52.         return $this->ciudad;
  53.     }
  54.     public function getProvincia()
  55.     {
  56.         return $this->provincia;
  57.     }
  58.     public function getPais()
  59.     {
  60.         return $this->pais;
  61.     }
  62.     public function getCodigoPostal()
  63.     {
  64.         return $this->codigoPostal;
  65.     }
  66.     public function getFechaNacimiento()
  67.     {
  68.         return $this->fechaNacimiento;
  69.     }
  70. }
  71. $posicion = $_GET["posicion"];
  72. switch ($posicion)
  73. {
  74.     case "0":
  75.         echo $indice;
  76.         break;
  77.     case "1":
  78.         $contactos[1];
  79.         break;
  80. }
  81.        
  82. ?>
  83. </body>
Página agenda:
Código PHP:
Ver original
  1. <body>
  2. <?php
  3. include_once("persona.inc.php");
  4. class agenda
  5. {
  6.     private $contactos = array();
  7.     function __construct()
  8.     {
  9.         $this->contactos[0] =new Persona("Juan", "Pérez", 666666666, "[email protected]", "direccion 1", "malaga", "malaga", "españa", 29000, "01/01/0000");
  10.         $this->contactos[1] =new Persona("Gema", "Soldado", 689478426, "[email protected]", "salvatierra 3", "Valencia", "Valencia", "España", 15630, "05/09/1975");
  11.         $this->contactos[2] =new Persona("Andrés", "Rio", 675984123, "[email protected]", "alatriste 5", "Granada", "Granada", "España", 18000, "03/07/1986");
  12.     }
  13.     public function getContactos()
  14.     {
  15.         return $this->contactos;
  16.     }
  17.     public function getPersona($indice)
  18.     {
  19.     return $this->contactos[$indice];
  20.     }
  21.     public function getIndice()
  22.     {
  23.         return $this->indice;
  24.     }
  25. }
  26.  
  27. ?>
  28. </body>
Página index:
Código PHP:
Ver original
  1. <body>
  2. <?php
  3.     include_once("agenda.inc.php")
  4. ?>
  5.    
  6. <table width="200" border="1">
  7. <?php
  8.     $agenda =new agenda()
  9. ?>
  10.   <tr>
  11.     <th scope="col"><h1>Nombre</h1></th>
  12.     <th scope="col"><h1>Apellidos</h1></th>
  13.     <th scope="col"><h1>Teléfono</h1></th>
  14.     <th scope="col"><h1>Detalles</h1></th>
  15.   </tr>
  16.   <tr>
  17.     <td><?php echo $agenda->getPersona(0)->getNombre(); ?></td>
  18.     <td><?php echo $agenda->getPersona(0)->getApellidos(); ?></td>
  19.     <td><?php echo $agenda->getPersona(0)->getTelefono(); ?></td>
  20.     <td><?php echo "<a href='persona.inc.php?posicion=0'>Ver</a>"; ?></td>
  21.   </tr>
  22.   <tr>
  23.     <td><?php echo $agenda->getPersona(1)->getNombre(); ?></td>
  24.     <td><?php echo $agenda->getPersona(1)->getApellidos(); ?></td>
  25.     <td><?php echo $agenda->getPersona(1)->getTelefono(); ?></td>
  26.     <td><?php echo "Ver"; ?></td>
  27.   </tr>
  28.   <tr>
  29.     <td><?php echo $agenda->getPersona(2)->getNombre(); ?></td>
  30.     <td><?php echo $agenda->getPersona(2)->getApellidos(); ?></td>
  31.     <td><?php echo $agenda->getPersona(2)->getTelefono(); ?></td>
  32.     <td><?php echo "Ver"; ?></td>
  33.   </tr>
  34.  
  35. </table>
  36.  
  37. </body>
Gracias por todo.