Page 1 of 1

Using Reports with the FPDF library in APPGINI

Posted: 2023-08-09 17:46
by vsandoval
I'm using the FPDF report library and it works great. The implementation is simple and it doesn't give me any kind of problem. The only thing I haven't been able to do is filter the reports by logged user. If anyone can help me I would greatly appreciate it.

Below is the code of one of my reports


<?php

define('PREPEND_PATH', '../');
$hooks_dir = dirname(__FILE__);
include("$hooks_dir/../defaultLang.php");
include("$hooks_dir/../language.php");
include("$hooks_dir/../lib.php");


// Incluimos la librería FPDF
require('fpdf/fpdf.php');

// Conectamos a la base de datos
$con = mysqli_connect("localhost","demo","0651Vrsl.","demo");

// Obtenga el ID del usuario autenticado
$user_id = $_SESSION['user_id'];

// Realizamos la consulta de los miembros
$query = "SELECT * FROM clientes WHERE user_id = $currentUserID";
$result = mysqli_query($con, $query);

// Creamos el documento PDF
$pdf = new FPDF();
$pdf->AddPage();

// Agregamos el título del reporte
$pdf->SetFont('Arial','',12);
$pdf->Cell(190,10,'REPORTE DE CLIENTES',0,1,'C');

$pdf->Ln(2);

// Agregamos los encabezados de la tabla
$pdf->SetFont('Arial','B',8);
$pdf->Cell(20,5,'CEDULA',1,0,'C');
$pdf->Cell(40,5,'NOMBRE',1,0,'C');
$pdf->Cell(30,5,'FECHA',1,0,'C');
$pdf->Cell(80,5,'DETALLE',1,0,'C');
$pdf->Cell(20,5,'MONTO',1,1,'C');

$pdf->SetFont('Arial','',8);

// Agregamos los datos de los miembros
while ($row = mysqli_fetch_array($result)) {
$pdf->Cell(20,5,$row['CEDULA'],1,0,'C');
$pdf->Cell(40,5,$row['NOMBRE'],1,0);
$pdf->Cell(30,5,$row['FECHA'],1,0,'C');
$pdf->Cell(80,5,$row['DETALLE'],1,0);
$pdf->Cell(20,5,$row['MONTO'],1,1);
}

$pdf->Output();
?>



This report works if I remove the code "WHERE user_id = $currentUserID" some way to implement so that it works per logged in user, Thanks !!!

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-08-09 19:20
by jsetzer
Thanks for your contribution!
// Obtenga el ID del usuario autenticado
$user_id = $_SESSION['user_id'];
You can get memberID of currently logged-in user like this:

Code: Select all

$user_id = getLoggedMemberID();
For your query it is important that values of clientes.user_id match up with AppGini-users' memberIDs from membership_users.memberID.

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-08-09 20:34
by vsandoval
Made the modifications that you suggested and I still can't filter by user. I'm going to include the modifications that I made to see if you can help me. Thank you in advance.

Code: Select all

// We connect to the database
$con = mysqli_connect("localhost","demo","0651Vrsl.","demo");

// Get the ID of the authenticated user
$user_id = getLoggedMemberID();

// We carry out customer consultation
$query = "SELECT * FROM clientes WHERE $user_id = '{$memberID}'";
$result = mysqli_query($con, $query);

// Creamos el documento PDF
$pdf = new FPDF();
$pdf->AddPage();

Forgive my ignorance coding
Thank you so much

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-08-09 21:00
by jsetzer
$user_id = getLoggedMemberID();
$query = "SELECT * FROM clientes WHERE $user_id = '{$memberID}'";
If you assign the memberID to a variable $user_id you have to use that variable afterwards, not a different variable.

Assuming table clientes has a column named user_id:

Code: Select all

// ...
$user_id = getLoggedMemberID();
// ...
$query = "SELECT * FROM clientes WHERE user_id = '{$user_id}'";
// ...
There can be many reasons. I strongly recommend adding logging to see intermediate variable values, for example:

Code: Select all

var_dump($user_id); exit;
Also, you should check user_id and memberID values in both tables using your preferred database tool (like adminer or phpMyAdmin).

Is it the same database? Then you can use AppGini's built in sql() command and don't have to care about connection parameters.

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-08-09 22:03
by vsandoval
Your idea was great and very easy to implement. I added a field called " user_id " and every time I make a registration it saves the " memberID " in it and I can use it in any external query like the following:

Code: Select all

// ...
$user_id = getLoggedMemberID();
// ...
$query = "SELECT * FROM clientes WHERE user_id = '{$user_id}'";
// ...
here is the example of the report working
I really appreciate your help and your prompt response and solution.

Thanks a lot

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-09-04 07:58
by afayez
vsandoval wrote:
2023-08-09 17:46
I'm using the FPDF report library

This is interesting. :ugeek:
Please support and give us a link or any details for this nice pdf library
Thanks

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-12-23 14:59
by vsandoval
that is the original website and it has many examples and how to implement them

http://www.fpdf.org/

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-12-27 03:19
by zibrahim
Hi there,
Just sharing my experience. I have used FPDF and later moved to TCPDF.
Like FPDF, TCPDF lets you set up margins, headers, footers, etc. but the difference is that it lets you use simple HTML and embedded CSS for the main content of the page.
https://brianshim.com/webtricks/php-to-pdf/
Check out the TCPDF at
https://tcpdf.org

Have fun and Happy New Year!

Re: Using Reports with the FPDF library in APPGINI

Posted: 2023-12-31 10:12
by onoehring
Hi zibrahim,

thanks for sharing

Happy new year
Olaf