Ver Mensaje Individual
  #6 (permalink)  
Antiguo 25/01/2011, 16:32
oscarbt
 
Fecha de Ingreso: abril-2009
Ubicación: Colombia
Mensajes: 949
Antigüedad: 15 años
Puntos: 27
Respuesta: Crear PDF de una Consulta MYSQL, con varias variables PHP

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