FPDF with Mysql and PHP - Bringing Repeated Data

Asked

Viewed 543 times

0

I’m trying to set up a generator system Vouchers/Tickets

I have a table in SQL with the name Vouchers with multiple users and their respective passwords.

I call in each Cell the variable $serial_voucher and $senha_voucher

But it only shows the first line and applies in all Cell.

Thank you, I’m grateful if someone can give me that strength.

DADOS REPETIDOS

 <?php 

$con = mysqli_connect('localhost','root','');
mysqli_select_db($con,'voucherss');

            $query=mysqli_query($con,"select *from vouchers");
            while($data=mysqli_fetch_array($query)){

//A4 WIDTH : 219MM


require ('pdf/fpdf.php');

$pdf = new Fpdf ('p','mm','A4');

$pdf->AddPage();
$pdf->Image("imagemfundo.jpg", 0,0,210,295);


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


$pdf->SetY( 20.3 );///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );   ///LADOS

    $pdf->Cell(0,10,$data['serial_voucher'],0,0,'L');   /////ESQUERDA 1

$pdf->SetY( 28.1);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );   ///LADOS

    $pdf->Cell(0,10,$data['senha_voucher'],0,0,'L');   /////ESQUERDA 1



$pdf->SetY( 20.3);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 177.8 );  ///LADOS

    $pdf->Cell( 0,10, $data['serial_voucher'], 0, 0, '' );    /////DIREITA 1

$pdf->SetY( 28.4); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 177.8 ); ///LADOS 

    $pdf->Cell( 0,10, $data['senha_voucher'], 0, 0, '' );     /////DIREITA 1


$pdf->SetY( 78.8 ); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );  ///LADOS

    $pdf->Cell(0,10,$data['serial_voucher'],0,0,'L');   /////ESQUERDA 2

$pdf->SetY( 86.3); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );  ///LADOS

    $pdf->Cell(0,10,$data['senha_voucher'],0,0,'L');   /////ESQUERDA 2



$pdf->SetY( 78.5); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 178 );  ///LADOS

    $pdf->Cell( 0,10, $data['serial_voucher'], 0, 0, '' );    /////DIREITA 2

$pdf->SetY( 86.3);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 178 ); ///LADOS

    $pdf->Cell( 0,10, $data['senha_voucher'], 0, 0, '' );     /////DIREITA 2    



$pdf->SetY( 136.5);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );   ///LADOS

    $pdf->Cell(0,10,$data['serial_voucher'],0,0,'L');   /////ESQUERDA 3

$pdf->SetY( 144.3);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );   ///LADOS

    $pdf->Cell(0,10,$data['senha_voucher'],0,0,'L');   /////ESQUERDA 3



$pdf->SetY( 136.5);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 177.8 );  ///LADOS

    $pdf->Cell( 0,10, $data['serial_voucher'], 0, 0, '' );    /////DIREITA 3

$pdf->SetY( 144.4); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 177.8 ); ///LADOS 

    $pdf->Cell( 0,10, $data['senha_voucher'], 0, 0, '' );     /////DIREITA 3




$pdf->SetY( 194.8 ); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );  ///LADOS

    $pdf->Cell(0,10,$data['serial_voucher'],0,0,'L');   /////ESQUERDA 4

$pdf->SetY( 202.7); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );  ///LADOS

    $pdf->Cell(0,10,$data['senha_voucher'],0,0,'L');   /////ESQUERDA 4



$pdf->SetY( 194.8); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 178 );  ///LADOS

    $pdf->Cell( 0,10, $data['serial_voucher'], 0, 0, '' );    /////DIREITA 4

$pdf->SetY( 202.7);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 178 ); ///LADOS

    $pdf->Cell( 0,10, $data['senha_voucher'], 0, 0, '' );     /////DIREITA 4




$pdf->SetY( 252.8 ); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );  ///LADOS

    $pdf->Cell(0,10,$data['serial_voucher'],0,0,'L');   /////ESQUERDA 5

$pdf->SetY( 260.6); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 74 );  ///LADOS

    $pdf->Cell(0,10,$data['senha_voucher'],0,0,'L');   /////ESQUERDA 5



$pdf->SetY( 252.8); ///PRA CIMA OU PRA BAIXO
$pdf->SetX( 178 );  ///LADOS

    $pdf->Cell( 0,10, $data['serial_voucher'], 0, 0, '' );    /////DIREITA 5

$pdf->SetY( 260.6);///PRA CIMA OU PRA BAIXO
$pdf->SetX( 178 ); ///LADOS

    $pdf->Cell( 0,10, $data['senha_voucher'], 0, 0, '' );     /////DIREITA 5        





$pdf->Output();



    }?> 
  • Welcome, I recommend you to do the Tour, Knowing your problem, this is happening because it is inside the loop of repetition while, put out of the loop what is not to be repeated, type data that are entered manually by you. require ('pdf/fpdf.php'); wasn’t supposed to be inside the loop.

  • Okay, I was able to understand, I did some other small systems where I worked with the HTML columns, was it enough to insert While with <tr></tr> and its <td>? php echo $Rows['id'];? ></td> and ready. - But in this case, I need to configure the position of each Cell in the FPDF to fit exactly in the Input of the template.pdf for this reason I am repeating the variable in each Cell. I believe the problem is there, but how else could I do it?

  • What data is not to repeat? Place this line $pdf = new Fpdf ('p','mm','A4'); and that too $pdf->Output(); out of the while

  • Example. I have a table with Serial columns and password. User 1 | Password1 User 2 | Password 2 And so on, but for some mistake in the code is only bringing the first line, User 1 | Password 1, and in each voucher should go a different user and password. Is showing the correct data but I want you to show all and not only the first line.

  • You want the data in one PDF just ? If not, just put the lines require ('pdf/fpdf.php'); $pdf = new Fpdf ('p','mm','A4'); before the while and the line $pdf->Output(); after the while.

  • @Souza look, with your suggestion now seems to be improving, now it is writing different data but on each page, example Page 1 User 1 | Password 1 and repeats until the next page... Page 2. User 2 | Password 2 and repeats as well. Summarizing: You are listing all database data but on one page for each user.

  • Put the $pdf->Output(); within the while again to see if it helps. There’s no way I can test now if I wouldn’t talk exact to you.

  • I did as you suggested, but we returned to the point 0 hehe. It did not work.

  • Okay, strip again, put also before the while the line $pdf->AddPage(); for it is she who adds new page, now I see that you are calling several times $pdf->SetY and $pdf->SetX, loop and row by row of returned result, so if you repeat these class methods FPDF within the loop will only repeat the data of the current line and not the next one, so that it works you must create conditions, something thus then makes the sum to position the objects.

  • Blz, I have followed your guidelines and other web tutorials such as http://www.fpdf.org/en/script/script30.php http://www.fpdf.org/en/script/script14.php Follow my current code. https://repl.it/repls/UnwittingOverjoyedKentrosaurus For the time being, printing the data only on the first card of each page, it is also repeating the same information on the right and left sides. desperation beating rsrsrs

Show 5 more comments

1 answer

0


I tweaked your code but I believe it is in the result you expect, just making the changes as you need it, I believe you can have easier ways to get the result. Down with the working code:

require ('fpdf/fpdf.php');

$con = mysqli_connect('localhost','root','');
mysqli_select_db($con,'voucherss');

$pdf = new Fpdf ('p','mm','A4');
$pdf->SetAutoPageBreak(false);
$pdf->SetFont('Arial','','16');
$pdf->AddPage();

//////INICIA COM ESSAS DIMENSOES
$yincrementasenha = 28.5;
$yincrementaserial = 20.3;

$linhas = 6; // Máximo de linhas por página
$coluna = 1; // Contagem de colunas
$count_lines = 0; // Contagem de linhas na página

$query=mysqli_query($con,"select * from vouchers");

while($data=mysqli_fetch_array($query)){
    $serial = $data['serial_voucher'];
    $senha = $data['senha_voucher'];

    if ($coluna === 1){
        $pdf->SetY( $yincrementaserial ); # Define a posião para Cima ou para Baixo
        $pdf->Cell(95,10, $serial,0,0,'L'); # Conteúdo do lado Esquerdo
        $pdf->SetY( $yincrementasenha ); # Define a posião para Cima ou para Baixo
        $pdf->Cell(95,10, $senha,0,0,'L'); # Conteúdo do lado Esquerdo
        $coluna++; // Faz incremento na contagem de colunas
    } elseif ($coluna === 2) {
        $count_lines++; // Faz incremento na contagem de linhas
        $pdf->SetY( $yincrementaserial ); # Define a posião para Cima ou para Baixo
        $pdf->SetX( 110 ); # Define a posição para Esquerda ou para Direita
        $pdf->Cell(95,10, $serial,0,0,'L'); # Conteúdo do lado Direito
        $pdf->SetY( $yincrementasenha ); # Define a posião para Cima ou para Baixo
        $pdf->SetX( 110 ); # Define a posição para Esquerda ou para Direita
        $pdf->Cell(95,10, $senha,0,0,'L'); # Conteúdo do lado Direito
        $coluna = 1; //  Reset a contagem de colunas
        $yincrementaserial = $yincrementaserial+24.4;
        $yincrementasenha = $yincrementasenha+24.4;
    }
    /*
     * Condição para verifcar se o números de linhas na página atual
     * é maior que cinco, se for adiciona nova página e reset as variáveis
     */
    if ($count_lines > 5){
        $pdf->AddPage(); // Adiciona nova página
        // Reset as variáveis para seus valores iniciais
        $count_lines = 0;
        $yincrementasenha = 28.5;
        $yincrementaserial = 20.3;
    }
}
$pdf->Output();

Some screens of the working code:

Exemplo funcionando: Página 1 Exemplo funcionando: Página 2

  • Man, that was good. Thanks for the help, there was a change I needed to make in your code besides my customizations, in the part of adding the page has to be > 4 and not 5. When I was jumping to the next page I didn’t add the following Row, example : 5 jumped pro 7 But I continue with the incial problem, the Script continues only by cloning the left column, I need the information to be different in each column, each voucher with different serial. COLUMN 1 : SERIAL1 SERIAL3 | COLUMN 2 : SERIAL2 SERIAL4 I found a staff discussing this (https://goo.gl/kqADqqV)

  • @Michelteixeira, sorry it was my mistake, I will update the answer, because at the time I was testing was right, more when I went to put here I made a change and had not noticed that I was putting the column of the same left right. The problem was in condition if in which I was not doing the elseif.

  • Already checked and now yes, it is in the perfect state of the art! Thank you very much.

  • I’ve already got the script well advanced, thank you very much for your first help, I’d like you to help me with one more thing. With the code you created, I’m having a problem.. At the time of displaying for example 50 vouchers, in logic would be 5 pages. However on the fifth page the last two vouchers are empty, I made an edition in the code and managed to fill everything, but is adding an extra page, blank. Help! :(

  • @Michelteixeira for this question here I can not help, I recommend ask a new question and there we check what is happening, put in the new question the link to this answer.

  • Okay, thanks for the info, I created the question here >>> https://answall.com/questions/256667/fpdf-com-php-ultima-p%C3%A1gina-in-white

Show 1 more comment

Browser other questions tagged

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