Ver Mensaje Individual
  #10 (permalink)  
Antiguo 25/01/2011, 18:16
hiramhzr
 
Fecha de Ingreso: enero-2011
Ubicación: En un Cuarto Cubierto de Pasto Verde
Mensajes: 95
Antigüedad: 13 años, 3 meses
Puntos: 3
Respuesta: Crear PDF de una Consulta MYSQL, con varias variables PHP

Cita:
Iniciado por oscarbt Ver Mensaje
Ok va un ejemplo sencillo:
Tengo dos tablas:

egresado:

Código SQL:
Ver original
  1. CREATE TABLE `egresado` (
  2.   `NUM_CED_EGR` INT(11) NOT NULL,
  3.   `COD_PRO` INT(11) NOT NULL,
  4.    `NOM_EGR` CHAR(25) DEFAULT NULL,
  5.   `APE_EGR` CHAR(25) DEFAULT NULL,
  6.  
  7.   PRIMARY KEY  (`NUM_CED_EGR`),
  8.   KEY `FK_PROGRAMA_EGRESADO` (`COD_PRO`)
  9.  
  10. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Fijate que el campo COD_PRO es foraneo

Ahora mi segunda tabla.

programa:

Código SQL:
Ver original
  1. CREATE TABLE `programa` (
  2.   `COD_PRO` INT(11) NOT NULL,
  3.  
  4.   `NOM_PRO` CHAR(50) DEFAULT NULL,
  5.   PRIMARY KEY  (`COD_PRO`)
  6.  
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Ahora tengo un formulario con selects donde selecciono el programa para posteriormente mostrar las personas que pertenecen a dicho programa:

entrada.php

Código PHP:
Ver original
  1. <form action="informepdf.php" method="post" name="form1" id="form1" >
  2.  
  3. Seleccione un programa
  4.  
  5. <?
  6. include ("funciones.php"); //ACA APARCEN LOS DATOS DE USUARIO, CONTRASEÑA, SERVIDOR
  7.    
  8. $bs = conectar ( "egresados" );
  9.        
  10.     echo "<select name= 'programa'>
  11.         <option value=0 selected>Seleccione el programa</option>";
  12.  
  13.     $qry = "SELECT * FROM programa";
  14.     $resultado = mysql_query($qry, $bs);
  15.     if ( !resultado )
  16.         die( "Error ejecutando la Consulta");
  17.        
  18.     $i=1;
  19.     $numero_filas = mysql_num_rows($resultado);
  20.  
  21.     while($i <= $numero_filas){
  22.         $fila = mysql_fetch_array( $resultado );
  23.         echo "<option value='$fila[COD_PRO]'> $fila[NOM_PRO] </option>";
  24.        
  25.         $i = $i + 1;
  26.     }
  27.     echo "</select>";
  28.     mysql_close ( $bs );
  29.    
  30. ?>
  31.  
  32. //Ahora el boton
  33.  
  34.  <input type="submit" name="enviar" id="enviar" value="Generar informe" />

Y finalmente el archivo informepdf.php



Código PHP:
Ver original
  1. <?
  2.     include('class.ezpdf.php');
  3.    
  4.    
  5.     $programa = $_POST['programa'];
  6.    
  7.     //Orientacion del documento vertical='portrait' o horizontal='landscape'
  8.     $pdf =& new Cezpdf('a4');//Crea el PDF en orientacion vertical
  9.     $pdf->selectFont('fonts/Broadsheet.afm');
  10.     $pdf->ezSetCmMargins(4,3,3,3);
  11.  
  12.     @ $db = mysql_connect('localhost', 'root', 'root');
  13.     mysql_select_db('egresados',$db);
  14.    
  15.     $query = "SELECT e.NUM_CED_EGR, e.NOM_EGR, e.APE_EGR, p.NOM_PRO from
  16.           egresado e, programa p
  17.           where p.COD_PRO= e.COD_PRO and e.COD_PRO= ".$programa."
  18.  
  19. ";
  20.  
  21.  
  22.    $result = mysql_query($query,$db) or die(mysql_error());
  23.    $num_results = mysql_num_rows($result);
  24.    
  25.    if($num_results==0){
  26.    
  27.    
  28.         echo '<h1> <center>RESULTADOS DE INFORME </center></h1>';
  29.         echo "<br>";
  30.        
  31.     echo '<h2> No se ha encontrado información  para generar un informe  </h2>';
  32.    
  33.    
  34.    
  35.     echo "<h2><a href='informes.php' a style='text-decoration:none;' > <p><b> Regresar a la sección de Informes</b></h2></a></p>";
  36.     exit;
  37.     }
  38.  
  39.     $i=0;
  40.  
  41.    
  42.     $nom_pro=0;
  43.    
  44.     while($row = mysql_fetch_assoc($result)){
  45.       $i++;
  46.      
  47.       $programa=$row['nomprogra'];
  48.      
  49.       $datos_tabla[] = array_merge($row, array('num'=>$i));
  50.      
  51.     }
  52.    
  53.    
  54.     $titulos_tabla = array(
  55.                 'num'=>'<b>No</b>',
  56.                 'NUM_CED_EGR'=>'<b>Cedular</b>',
  57.                 'NOM_EGR'=>'<b>Nombres</b>',
  58.                     'APE_EGR'=>'<b>Apellidos</b>'
  59.                
  60.                        
  61.                        
  62.        );
  63.      
  64.      
  65.      
  66.      
  67.     $pdf->ezText("<b>INFORME DE INVESTIGADORES POR PROGRAMA</b>\n",18);
  68.     $pdf->ezText("<b>Programa:  $programa </b>\n",14);
  69.    
  70.    
  71.     $pdf->ezTable($datos_tabla,$titulos_tabla,$options);
  72.     $pdf->ezText("\n\n\n", 10);
  73.     $pdf->ezText("<b>Fecha:</b> ".date("d/m/Y"), 10);
  74.     $pdf->ezText("<b>Hora:</b> ".date("H:i:s")."\n\n", 10);
  75.     $pdf->ezStream();
  76.  
  77.  
  78. ?>

Espero te sirva, saludos

Hola oscarbt muchisimas gracias por tu codigo.

Lo intente correr y me generaba un error en la primer pantalla lo modifique y ya me funciona asi me quedo el codigo.
Código PHP:
Ver original
  1. <?php
  2. include('conexion.php');
  3. $link = conectarse();
  4. ?>
  5. <html>
  6. <body>
  7. <form action="informepdf.php" method="post" name="form1" id="form1" >
  8. <b>Area:
  9.        
  10.         <?php
  11.             // Realizar una consulta SQL
  12.             $consulta = 'SELECT * FROM programa';
  13.             $resultado = mysql_query($consulta) or die('La consulta falló: ' . mysql_error());
  14.  
  15.             // Impresion de resultados en HTML
  16.             echo "<select name=\"area\">\n";
  17.             while ($linea = mysql_fetch_array($resultado, MYSQL_ASSOC)) {
  18.             $area = $linea["NOM_PRO"];
  19.             $idArea = $linea["COD_PRO"];
  20.             echo "<option value=".$idArea.">".$area."</option>\n";
  21.             }
  22.                 $res=$row["area"];
  23.                 echo "</select>\n";
  24.                 mysql_free_result($resultado);           
  25.                
  26.             ?>  
  27.  
  28.         <br><br>           
  29.                
  30.         <input type="submit" value="enviar">
  31. </form>
  32. </body>
  33. </html>

El problema es que al hacer submit no me carga el otro archivo y me marca el siguiente error:
Código HTML:
Parse error: syntax error, unexpected T_INCLUDE in C:\xampp\htdocs\busqueda1\informepdf.php on line 2
y mi el codigo de ese archvo es el siguiente:
Código PHP:
Ver original
  1. <?php
  2. *    include('class.ezpdf.php');
  3. * * //Orientacion del documento vertical='portrait' o horizontal='landscape'
  4. * * $pdf =& new Cezpdf('a4');//Crea el PDF en orientacion vertical
  5. * * $pdf->selectFont('fonts/Broadsheet.afm');
  6. * * $pdf->ezSetCmMargins(4,3,3,3);
  7. *
  8. * * @ $db = mysql_connect('localhost', 'root', '');
  9. * * mysql_select_db('ejemplo',$db);
  10. * * $programa = $_POST['area'];* *
  11. * * $query = "SELECT e.NUM_CED_EGR, e.NOM_EGR, e.APE_EGR, p.NOM_PRO from
  12. * * * * * egresado e, programa p
  13. * * * * * where p.COD_PRO= e.COD_PRO and e.COD_PRO='$programa";
  14. *
  15. *
  16. * *$result = mysql_query($query,$db) or die(mysql_error());
  17. * *$num_results = mysql_num_rows($result);
  18. * *
  19. * *if($num_results==0){
  20. * *
  21. * *
  22. * * * * echo '<h1> <center>RESULTADOS DE INFORME </center></h1>';
  23. * * * * echo "<br>";
  24. * * * *
  25. * * echo '<h2> No se ha encontrado información *para generar un informe *</h2>';
  26. * *
  27. * *
  28. * *
  29. * * echo "<h2><a href='informes.php' a style='text-decoration:none;' > <p><b> Regresar a la sección de Informes</b></h2></a></p>";
  30. * * exit;
  31. * * }
  32. *
  33. * * $i=0;
  34. *
  35. * *
  36. * * $nom_pro=0;
  37. * *
  38. * * while($row = mysql_fetch_assoc($result)){
  39. * * * $i++;
  40. * * *
  41. * * * $programa=$row['nomprogra'];
  42. * * *
  43. * * * $datos_tabla[] = array_merge($row, array('num'=>$i));
  44. * * *
  45. * * }
  46. * *
  47. * *
  48. * * $titulos_tabla = array(
  49. * * * * * * * * 'num'=>'<b>No</b>',
  50. * * * * * * * * 'NUM_CED_EGR'=>'<b>Cedular</b>',
  51. * * * * * * * * 'NOM_EGR'=>'<b>Nombres</b>',
  52. * * * * * * * * * * 'APE_EGR'=>'<b>Apellidos</b>' * * * * * * * *
  53. * * * * * * * * * * * *
  54. * * * *);
  55. * * *
  56. * * *
  57. * * *
  58. * * *
  59. * * $pdf->ezText("<b>INFORME DE INVESTIGADORES POR PROGRAMA</b>\n",18);
  60. * * $pdf->ezText("<b>Programa: *$programa </b>\n",14);
  61. * *
  62. * *
  63. * * $pdf->ezTable($datos_tabla,$titulos_tabla,$options);
  64. * * $pdf->ezText("\n\n\n", 10);
  65. * * $pdf->ezText("<b>Fecha:</b> ".date("d/m/Y"), 10);
  66. * * $pdf->ezText("<b>Hora:</b> ".date("H:i:s")."\n\n", 10);
  67. * * $pdf->ezStream();
  68. *
  69. *
  70. ?>

Seuire intentando mientras tanto muchisimas gracias por tus rspuestas. Saludos