Change PDF background according to database data (PHP, MYSQL, FPDF)

Asked

Viewed 198 times

1

I’m trying to personalize my report on FPDF, It is a system that generates discount tickets, where they are printed in batches, for example batch 50 units. Tickets in this batch will have the same profile where it can be 10%, 20% and 30% off. I would like to customize the background according to the profile.

I would like to know the profile of only one of the tickets to make the comparison since the profile is the same for all of the lot

$result_tickets = "SELECT * FROM vouchers WHERE serial_lote = '112027511417'";
$resultado = mysqli_query($conn, $result_tickets);

$registros = mysqli_num_rows($resultado);

////ME VEIO ESTA IDEIA, PORÉM NÃO ESTÁ FUNCIONANDO..

$perfil_desconto = $resultado['desconto'];
if ($perfil_desconto == '10%') {

   $template_background = "template-10.jpg";

} elseif ($perfil_desconto == '20%') {

   $template_background = "template-20.jpg";

}  elseif ($perfil_desconto == '30%') {

   $template_background = "template-30.jpg";

}  elseif ($perfil_desconto == '50%') {

   $template_background = "template-50.jpg";

} 

$pdf = new Fpdf ('p','mm','A4');
$pdf->SetAutoPageBreak(false);
$pdf->SetFont('Arial','','10');
$pdf->AddPage();
$pdf->Image("$template_background", 0,0,210,295);

Remembering that you still have While that pulls all the data from the database and inserts it in the PDF, but I didn’t think it was necessary to insert it here.

  • Why do you say it’s not working? There’s not coming the full content?

  • @Andreicoelho see well, everything is ok.. I just want to customize the sheet according to the profile.. something like: Case (profile)

  • 1

    @Andreicoelho is right yes, it is a url, so it is necessary.

  • Is that I still don’t understand the right question. The profile you’re talking about is the discount? Profile x is 20%, profile y is 30%, that’s it?

  • 1

    @Andreicoelho exactly, each print picks up a batch, this batch has ticket’s of only one profile, need to use a template for each profile.

  • $template_voucher = "template-10.jpg"; Is this variable right? Or would it be template_background ?

  • 1

    @Andrei know this, actually at the time I posted my question I must have inserted wrong, I made a few edits.. I will correct.

  • Quiet. The mistake is that it is not generating the PDF. That’s it?

  • 1

    @Andreicoelho is generating everything normal, only I’m using the same background (Background) for all profiles.... you know? What I’m trying to do is use a different background image for each profile.

  • Got it now. I’ll formulate a response.

  • Each voucher is an A4 sheet? Or fit more? Type, each A4 sheet is 4 voucher.

  • 1

    @Andreicoelho on each A4 sheet comes 10 vouchers has two columns, 5 on each side, but would need to check the profile of only one to identify since all of the lot are of the same profile.

Show 7 more comments

1 answer

1


The problem is on this line $perfil_desconto = $resultado['desconto'];. You cannot identify because the variable $resultado is related to the query object.

One option would be to add the mysqli_fetch_array do this:

$resultado = mysqli_query($conn, $result_tickets);

$registros = mysqli_num_rows($resultado);

$reg = mysqli_fetch_array($resultado); // aqui eu adicionei

////ME VEIO ESTA IDEIA, PORÉM NÃO ESTÁ FUNCIONANDO..

$perfil_desconto = $reg['desconto'];
if ($perfil_desconto == '10%') {

   $template_background = "template-10.jpg";

} elseif ($perfil_desconto == '20%') {

   $template_background = "template-20.jpg";

}  elseif ($perfil_desconto == '30%') {

   $template_background = "template-30.jpg";

}  elseif ($perfil_desconto == '50%') {

   $template_background = "template-50.jpg";

} 

If it’s not that, comment here.

  • 1

    Perfect, it was just that! Thanks young!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.