Generate a report in PDF in PHP AND MYSQL

1

Good evening I make a report in PDF with PHP and MYSQL of what my table of my BD contains are:

* (main table) "worker" with the attributes control_id, name, ap_parter, ap_maternal, NSS, CURP, RFC and with the foreign keys id_puesto, id_area, idctg_turno, idctg_empresa

* Table "area" with the attributes id_area, descripcion_area

* Table "ctg_turno" with attributes idctg_turno, description

* Table "company" with the attributes idctg_empresa, empresa_description.

What I want my report to do in PDF is that it takes the descriptions of the IDs of my foreign keys, that is, if the id_area is equal to 1 that shows the description

<?php

require_once('../lib/pdf/mpdf.php');
$conn = new mysqli('localhost','root','','admon');
$query = ("SELECT t.id_control, t.nombre, t.ap_paterno, t.ap_materno, t.NSS, t.CURP, t.RFC, p.descripcion_puesto, a.descripcion_area, tu.descripcion, e.descripcion_empresa FROM trabajador t INNER JOIN puesto p ON t.id_puesto = p.id_puesto INNER JOIN area a ON t.id_area = a.id_area INNER JOIN ctg_turno tu ON t.idctg_turno = tu.idctg_turno INNER JOIN empresa e ON t.idctg_empresa = e.idctg_empresa");
$prepare = $conn->prepare($query);
$prepare->execute();
$resulSet = $prepare->get_result();
while($trabajador[] = $resulSet->fetch_array());
$resulSet->close();
$prepare->close();
$conn->close();






$html = '<header class="clearfix">
      <div id="logo">
        <img src="img/logo.png">
      </div>
      <h1>Reporte de Trabajadores Registrados</h1>
    </header>
    <main>
      <table>
        <thead>
          <tr>
		  <th>No. de Control</th>
            <th>Nombre</th>
			<th>Apellido Paterno</th>
			<th>Apellido Materno</th>
			<th>NSS</th>
			<th>CURP</th>
			<th>RFC</th>
			<th>Puesto</th>
			<th>Área</th>
			<th>Turno</th>
			<th>Empresa</th>
	
          </tr>
        </thead>
        <tbody>';
		
		foreach ($trabajador as $trabajador){
		$html.='<tr>
		
		            <td class="qty">'.$trabajador['id_control'].'</td>
                    <td class="qty">'.$trabajador['nombre'].'</td>
					<td class="qty">'.$trabajador['ap_paterno'].'</td>
					<td class="qty">'.$trabajador['ap_materno'].'</td>
					<td class="qty">'.$trabajador['NSS'].'</td>
					<td class="qty">'.$trabajador['CURP'].'</td>
					<td class="qty">'.$trabajador['RFC'].'</td>
					<td class="qty">'.$trabajador['id_puesto'].'</td>
					<td class="qty">'.$trabajador['id_area'].'</td>
					<td class="qty">'.$trabajador['idctg_turno'].'</td>
					<td class="qty">'.$trabajador['idctg_empresa'].'</td>
                    
                    </tr>';
		
			
			
			
		}
		
          $html.='
        </tbody>
      </table>
      <div id="notices">
        <div>Aviso:</div>
        <div class="notice">Este documento es propiedad de Concorde Group S.A de C.V.</div>
      </div>
    </main>';

$mpdf = new mPDF('c','A4');
$css = file_get_contents('css/style.css');
$mpdf->writeHTML($css,1);
$mpdf->writeHTML($html);
$mpdf->Output('reporte.pdf','I');








?>

company_id is equal to TIP (which contains site_id = 1). I try to do it with a JOIN.

Inquiry

SELECT 
  t.id_control, 
  t.nombre, 
  t.ap_paterno, 
  t.ap_materno, 
  t.NSS, 
  t.CURP, 
  t.RFC, 
  p.descripcion_puesto, 
  a.descripcion_area, 
  tu.descripcion, 
  e.descripcion_empresa 
FROM trabajador t 
INNER JOIN puesto p 
  ON t.id_puesto = p.id_puesto 
INNER JOIN area a 
  ON t.id_area = a.id_area 
INNER JOIN ctg_turno tu 
  ON t.idctg_turno = tu.idctg_turno 
INNER JOIN empresa e 
  ON t.idctg_empresa = e.idctg_empresa
    
asked by Oscar_DR 15.02.2018 в 06:11
source

1 answer

0

In the query you are asking MYSQL to return the following columns in the SELECT

SELECT 
  t.id_control, 
  t.nombre, 
  t.ap_paterno, 
  t.ap_materno, 
  t.NSS, 
  t.CURP, 
  t.RFC, 
  p.descripcion_puesto, 
  a.descripcion_area, 
  tu.descripcion, 
  e.descripcion_empresa 
--- resto de consulta ---

But then when you scroll through the results of the query you try to show different ones:

$html.='<tr>
                    <td class="qty">'.$trabajador['id_control'].'</td>
                    <td class="qty">'.$trabajador['nombre'].'</td>
                    <td class="qty">'.$trabajador['ap_paterno'].'</td>
                    <td class="qty">'.$trabajador['ap_materno'].'</td>
                    <td class="qty">'.$trabajador['NSS'].'</td>
                    <td class="qty">'.$trabajador['CURP'].'</td>
                    <td class="qty">'.$trabajador['RFC'].'</td>
                    <td class="qty">'.$trabajador['id_puesto'].'</td>
                    <td class="qty">'.$trabajador['id_area'].'</td>
                    <td class="qty">'.$trabajador['idctg_turno'].'</td>
                    <td class="qty">'.$trabajador['idctg_empresa'].'</td>

                    </tr>';

You must use the columns that you retrieved in the query, for example instead of $trabajador['id_puesto'] it should be $trabajador['descripcion_puesto'] .

    
answered by 15.02.2018 / 12:52
source