0

I am trying to output data in pdf. Upto 10 values code is working fine but it is not display 11th value and it is continuing from 12th value. Please help me fix it.

<?php
$result=mysqli_query($conn,"SELECT * FROM TABLE");
$Items = array();
while ($row = mysqli_fetch_assoc($result)) {
  $Items[] = $row;
}

$x=0;
$y=0;
$a=5;
$b=5;

require('../pdf/alphapdf.php');
$pdf = new AlphaPDF();

$pdf->AddPage();
$title = 'Demo';
$pdf->SetTitle($title);
$pdf->SetLineWidth(1.5);
$pdf->SetAlpha(1);

foreach ($Items as $array) {
if($x<5){
    $pdf->Image('abc.png',$a,$b,-300);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Text($a,$b,$array['userName']);
    $a=$a+58;
    $x++;
}elseif($x>=5 && $x<10){
    if($a>290){$a=5;}
    $b=100;
    $pdf->Image('abc.png',$a,$b,-300);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Text($a,$b,$array['userName']);
    $a=$a+58;
    $x++;
}else{
    $x=0;
    $a=5;
    $b=5;
    $pdf->AddPage();
 }
}
$pdf->Output();
?>

I know the issue is because of the else statement that i am using but i am not sure how to fix it.

3
  • If $x > 10, you hit the else statement where you're adding a new page. But you don't then actually output the 11th item, you're moving on to process the 12th instead. Commented Mar 12, 2021 at 9:28
  • try like this: 3v4l.org/bjUGl (use code of link) Commented Mar 12, 2021 at 9:32
  • @AlivetoDie Yes it did :) Commented Mar 12, 2021 at 9:45

2 Answers 2

1

When 11th record coming in your code, then your code goes to else part and there you have written code only to create a new page, nothing else. That's why 11th record got skipped.

Also you are using repeated code multiple time, which is not needed actually, try like this:

$x=0;
$a=5;
$b=5;
foreach ($Items as $array) {

    if($x %10 == 0 ){ // after each 10 record add new page
        $x=0;
        $a=5;
        $b=5;
        $pdf->AddPage();
    }
    //add entries irrespective of pages.
    $pdf->Image('abc.png',$a,$b,-300);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Text($a,$b,$array['userName']);
    $a=$a+58;
    $x++;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is very quick and short solution for your this problem. You have to use less than and equal to both as well. Kindly update you code line:

}elseif($x>=5 && $x<=10){

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.