J
The problem is that in your class PDF You're running this query:$consulta = "SELECT * FROM cliente";
And then creating the set of results of it to create the PDF. That query does not have any filters, bring all customers. If you want them filtered, you have to indicate the filter you want, in your query, something like:$consulta = "SELECT * FROM cliente WHERE -- Aquí los criterios de filtro";
On the other hand, your class PDF It's unprofitable as you have it now. Classes are usually created to be reused. But if you enter the class, both the connection to the DB, and a specific query, that class will not serve you to print other PDFs, which may come or not from the database and may be based on other tables, not just customers, or text files, or JSON, or HTML content.I will briefly show you a simple way to make your class a little more useful. What I've done has been to create a method called getClients() to get the customer list. If you then need a list of employees, you can add another method to the class that generates that list and you can do more complex things, such as generating any PDFs from one method, passing parameters and so on, but don't get into it now... I drop it simply to keep in mind that one of the great advantages of POO is power reuse code.This would be the class:require('fpdf/fpdf.php');
class PDF extends FPDF
{
/*Quitamos la conexión de aquí, lo mejor es pasarle los datos desde fuera*/
/* new PDF() no es necesario*/
/*Y usamos $this en todo esto,porque la clase extiende de FPDF*/
$this->AliasNbPages();
$this->AddPage();
/*
Este y otros parámetros pueden hacerse dinámicos,
así puedes usar la misma clase para generar cualquier PDF
con cualquier fuente y tamaño
*/
$this->SetFont('Arial','',16);
public function getClients($mData) {
foreach($mData as $row){
/*NÓTESE que usamos $this aquí también*/
$this->Cell(30, 10, $row['id'], 1, 0, 'C', 0);
$this->Cell(70, 10, $row['name'], 1, 0, 'C', 0);
$this->Cell(35, 10, $row['email'], 1, 0, 'C', 0);
}
/* Retornamos el PDF*/
return $this->Output();
}
}
?>
Now, from any context, such as the first code block of your question:Connecting to the DatabaseYou'd launch the consultation.You would include your class PDF and you'd call the method that generates the customer list.An example of contextual use would be just what you had in the body of the class. PDF and to tie it to that only context, making it useless for other contexts and thus breaking one of the purposes of the POO that is to reuse code.We're in another file, in any context.Let's say this is the 341 context in a huge application.. .require 'conexion.php';
requiere 'elNombreDeArchivoDeTuClasePDF.php';
/Para prueba, pedimos sólo 5 clientes/
$consulta = "SELECT * FROM cliente LIMIT 5";
$resultado = $mysqli->query($consulta);
/Este array guardará las filas del resultado/
$mData=array();
while($row = $resultado->fetch_assoc()) {
/Vamos llenando $mData con cada fila/
$mData[]=$row;
}
/Una vez lleno el array creamos la instancia de PDF/
$pdf = new PDF();
/Llamamos nuestro método/
$mPDF=$pdf->getClientes($mData);
/Usar $mPDF para lo que necesites/
It is important that you understand the difference between contextual use (in an application there may be billions of contexts) and what is a class. In class methods there may be simple operations (such as the example of getClientes() or very complex) and the purpose of the class is to encapsulate these processes, so then you don't have to break your coconut in those multiple contexts, defining how this has to be done or whatBecause you define it in class.Suppose you need a list of employees now, then you'd give the class PDF of a method getEmpleados() where you define how that list should be obtained. And, if you're in an application where thousands or millions of different things should be created, then you could be engineered to create a method that receives certain parameters and is able to generate any type of PDF list or document with that only method, so the PDF class has not a thousand methods, but one, two or three, that produce outputs of the same genre.Well, I've gone a little longer, but I think it's important to make the difference between contextual use and class in POO, but you quickly have a code spaghetti and find that you're not really progamate in POO, even if you use classes and so on.