Using Reports with the FPDF library in APPGINI

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
vsandoval
Posts: 9
Joined: 2023-01-14 15:51

Using Reports with the FPDF library in APPGINI

Post by vsandoval » 2023-08-09 17:46

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 !!!

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1792
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Using Reports with the FPDF library in APPGINI

Post by jsetzer » 2023-08-09 19:20

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.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

vsandoval
Posts: 9
Joined: 2023-01-14 15:51

Re: Using Reports with the FPDF library in APPGINI

Post by vsandoval » 2023-08-09 20:34

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

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1792
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Using Reports with the FPDF library in APPGINI

Post by jsetzer » 2023-08-09 21:00

$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.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

vsandoval
Posts: 9
Joined: 2023-01-14 15:51

Re: Using Reports with the FPDF library in APPGINI

Post by vsandoval » 2023-08-09 22:03

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
Attachments
reporte de clientes.JPG
reporte de clientes.JPG (33.38 KiB) Viewed 4432 times

User avatar
afayez
Posts: 12
Joined: 2020-09-06 15:24
Location: EGYPT
Contact:

Re: Using Reports with the FPDF library in APPGINI

Post by afayez » 2023-09-04 07:58

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

vsandoval
Posts: 9
Joined: 2023-01-14 15:51

Re: Using Reports with the FPDF library in APPGINI

Post by vsandoval » 2023-12-23 14:59

that is the original website and it has many examples and how to implement them

http://www.fpdf.org/

User avatar
zibrahim
Veteran Member
Posts: 135
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Using Reports with the FPDF library in APPGINI

Post by zibrahim » 2023-12-27 03:19

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!
Zala.
Appgini 23.16, MacOS 14.0 Windows 11 on Parallels.


Post Reply