Foros del Web » Programando para Internet » PHP »

PHP OO Recuperacion de datos

Estas en el tema de Recuperacion de datos en el foro de PHP en Foros del Web. Hola a todos, pues estoy empezando a practicar poo para desarrollar ya de esa forma mis proyectos y bueno pues que mejor forma que hacer ...
  #1 (permalink)  
Antiguo 15/12/2011, 14:01
Avatar de CesarHC  
Fecha de Ingreso: junio-2011
Ubicación: localhost
Mensajes: 566
Antigüedad: 12 años, 10 meses
Puntos: 56
Recuperacion de datos

Hola a todos, pues estoy empezando a practicar poo para desarrollar ya de esa forma mis proyectos y bueno pues que mejor forma que hacer un formulario que guarde, recupere, muestre y elimine XD, weno asi es como lo tengo.

config.php
Código PHP:
Ver original
  1. <?php
  2.  
  3. class config
  4. {  
  5.     var $server="localhost";
  6.     var $user="root";
  7.     var $pass="";
  8.     var $bd="reqcot";
  9.     var $sgbd="MySQL";
  10.     /////
  11.     public $consulta;//LA CONSULTA
  12.     /////
  13.    
  14.     function conectar()
  15.     {
  16.         switch($this->sgbd)
  17.         {
  18.         case 'MySQL':
  19.             $this->conexion = new mysqli($this->server,$this->user,$this->pass,$this->bd);
  20.             if(mysqli_connect_errno())
  21.             {
  22.                 printf("Error en la conexión %s\n", mysqli_connect_error());
  23.                 exit();
  24.             }
  25.             break;
  26.         }
  27.     }
  28.     function cerrar()
  29.     {
  30.         mysqli_close($this->conexion);
  31.     }
  32.     function consulta_guardar()
  33.     {
  34.         switch($this->sgbd)
  35.         {
  36.             case 'MySQL':
  37.                 $sqlg=$this->consulta;//DATOS DE LA CONSULTA
  38.                 $this->conexion->query($sqlg);//Ejecutamos la consulta
  39.                 return $sqlg;
  40.                 break;
  41.         }
  42.        
  43.     }
  44.     function consulta_mostrar()
  45.     {
  46.     switch($this->sgbd)
  47.         {
  48.             case 'MySQL':
  49.                 $sqlg=$this->consulta;//DATOS DE LA CONSULTA
  50.                 $result=$this->conexion->query($sqlg);//Ejecutamos la consulta
  51.                
  52. if ($row = mysql_fetch_array($result)){
  53. echo "
  54. <table>
  55.  <tr>
  56.    <td>Nombres</td>
  57.    <td>".$row["nombre"]."</td>
  58.  </tr>
  59. ";
  60.     while ($row = mysql_fetch_array($result));
  61.    echo "</table> \n";
  62. } else {
  63. echo "¡ No se ha encontrado ningún registro !";
  64. }
  65.  
  66.                
  67.                 break;
  68.         }
  69.    
  70.     }
  71. }
  72. ?>

cprueba.php
Código PHP:
Ver original
  1. <?php
  2. require_once("../config/config.php");
  3. class prueba
  4. {
  5.   public $consulta;
  6.   function prueba($idprueba,$nombre,$apellido,$grupo,$fecha)
  7.   {
  8.     $this->idprueba = $idprueba;
  9.     $this->nombre = $nombre;
  10.     $this->apellido = $apellido;
  11.     $this->grupo = $grupo;
  12.     $this->fecha = $fecha;
  13.     $this->mc = new config();
  14.   }
  15.   function guardar()
  16.   {
  17.     $this->mc->consulta ="INSERT INTO prueba VALUES('$this->idprueba','$this->nombre','$this->apellido','$this->grupo','$this->fecha')";
  18.     $this->mc->conectar();
  19.     $g = $this->mc->consulta_guardar();
  20.     $this->mc->cerrar();
  21.     return $g;
  22.   }
  23.   function mostrar()
  24.   {
  25.     $this->mc->consulta ="SELECT nombre FROM prueba ";
  26.     $this->mc->conectar();
  27.     $g = $this->mc->consulta_mostrar();
  28.     $this->mc->cerrar();
  29.     return $g;
  30.   }
  31. }
  32. ?>

frmprueba.php
Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Documento sin título</title>
  6. </head>
  7. <body>
  8. <?php
  9. require_once("../negocios/cprueba.php");
  10. $idprueba = $_REQUEST['idprueba'];
  11. $nombre = $_REQUEST['nombre'];
  12. $apellido = $_REQUEST['apellido'];
  13. $grupo = $_REQUEST['grupo'];
  14. $fecha = $_REQUEST['fecha'];
  15. $sub = $_REQUEST['sub'];
  16. $prueba = new prueba($idprueba,$nombre,$apellido,$grupo,$fecha);
  17. ?>
  18. <form action="frmprueba.php" method="post">
  19. <h1 align="center">Prueba</h1>
  20. <table align="center" id="datos">
  21.   <tr>
  22.     <th></th>
  23.     <td><input type="hidden" name="idprueba" size="30" value="<?php echo $idprueba; ?>"></td>
  24.   </tr>
  25.   <tr>
  26.     <th>Nombre</th>
  27.     <td><input type="text" name="nombre" id="sel5" size="30"  value="<?php echo $nombre; ?>" ></td>
  28.   </tr>
  29.   <tr>
  30.     <th>Apellido</th>
  31.     <td><input type="text" name="apellido" size="30" value="<?php echo $apellido; ?>"/></td>
  32.   </tr>
  33.     <th>Grupo</th>
  34.     <td><input type="text" name="grupo" size="30" value="<?php echo $grupo; ?>"/></td>
  35.   </tr>
  36.   <tr>
  37.     <th>Fecha</th>
  38.     <td><input name="fecha" type="text" value="<?php echo $fecha; ?>" size="40" /></td>
  39.   </tr>
  40.     <td colspan="2" align="center">
  41.       <input type="submit" name="sub" value="Guardar" id="button" />
  42.       <input type="submit" name="sub" value="Mostrar" id="button" />
  43.       </td>
  44.   </tr>
  45. </table>
  46. <br />
  47.  
  48.  
  49.       <?php
  50.       switch($sub)
  51.       {
  52.             case 'Guardar':
  53.                    echo $prueba->guardar();
  54.                    break;  
  55.             case 'Mostrar':
  56.                    echo $prueba->mostrar();
  57.                    break;
  58.       }
  59.       ?>
  60.  
  61. </form>
  62. </body>
  63. </html>

Bueno pues comenze con el guardar no me dio problemas, pero al querer recuperar datos con un select para listarlos no los puedo recuperar -_-, agradeceria que me dijeran que estoy haciendo mal en "function consulta_mostrar() y function mostrar()" aun me pierdo un poco con estos esquemas XD espero su consejo gracias.
__________________
Solo la práctica no te traicionara ¡¡¡¡¡¡

Seguir el camino tu debes PHP The Right Way.
  #2 (permalink)  
Antiguo 15/12/2011, 14:22
Avatar de Patriarka  
Fecha de Ingreso: enero-2011
Ubicación: Moreno, Buenos Aires, Argentina
Mensajes: 2.851
Antigüedad: 13 años, 2 meses
Puntos: 288
Respuesta: Recuperacion de datos

por que no escapas al html?
Código PHP:
Ver original
  1. if ($row = mysql_fetch_array($result)){
  2. ?>
  3. <table>
  4. <?php
  5.     while ($row = mysql_fetch_array($result)){
  6. ?>
  7. <tr>
  8.    <td>Nombres</td>
  9.    <td><?php echo $row["nombre"]; ?></td>
  10.  </tr>
  11. <?php
  12. }
  13. ?>
  14. </table>
  #3 (permalink)  
Antiguo 15/12/2011, 14:53
Avatar de CesarHC  
Fecha de Ingreso: junio-2011
Ubicación: localhost
Mensajes: 566
Antigüedad: 12 años, 10 meses
Puntos: 56
Respuesta: Recuperacion de datos

Gracias por responder, ya escape el html pero me tira este error.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\POO\config\config.php on line 52
� No se ha encontrado ning�n registro !

Parece que los datos no llegan bien al fecth revisare.
__________________
Solo la práctica no te traicionara ¡¡¡¡¡¡

Seguir el camino tu debes PHP The Right Way.
  #4 (permalink)  
Antiguo 16/12/2011, 08:45
Avatar de Patriarka  
Fecha de Ingreso: enero-2011
Ubicación: Moreno, Buenos Aires, Argentina
Mensajes: 2.851
Antigüedad: 13 años, 2 meses
Puntos: 288
Respuesta: Recuperacion de datos

Cita:
Iniciado por Patriarka Ver Mensaje
por que no escapas al html?
Código PHP:
Ver original
  1. if (mysql_num_rows($result) > 0){
  2. ?>
  3. <table>
  4. <?php
  5.     while ($row = mysql_fetch_array($result)){
  6. ?>
  7. <tr>
  8.    <td>Nombres</td>
  9.    <td><?php echo $row["nombre"]; ?></td>
  10.  </tr>
  11. <?php
  12. }
  13. ?>
  14. </table>
  #5 (permalink)  
Antiguo 16/12/2011, 10:07
Avatar de CesarHC  
Fecha de Ingreso: junio-2011
Ubicación: localhost
Mensajes: 566
Antigüedad: 12 años, 10 meses
Puntos: 56
Respuesta: Recuperacion de datos

Hola solo para decir que ya lo arregle, me habia equivocado con el mysqli, gracias por tomarse el tiempo de escribir en este post XD.

Código PHP:
Ver original
  1. function consulta_mostrar()
  2.     {
  3.     switch($this->sgbd)
  4.         {
  5.             case 'MySQL':
  6.                 $sqlg=$this->consulta;//DATOS DE LA CONSULTA
  7.                 $re=$this->conexion->query($sqlg);//Ejecutamos la consulta
  8.                 while ($fila = $re->fetch_array())
  9.            {
  10.             echo $fila["nombre"];
  11.            }
  12.  
  13.         }
  14.  
  15.                 return $re;
  16.                 break;
  17.     }

Código PHP:
Ver original
  1. function mostrar()
  2.   {
  3.     $this->mc->consulta ="SELECT nombre FROM prueba";
  4.     $this->mc->conectar();
  5.     $g = $this->mc->consulta_mostrar();
  6.     $this->mc->cerrar();
  7.     return $g;
  8.   }
__________________
Solo la práctica no te traicionara ¡¡¡¡¡¡

Seguir el camino tu debes PHP The Right Way.

Etiquetas: formulario, html, mysql, recuperacion, registro, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:56.