Vikash Kumar
Vikash Kumar

Reputation: 33

Issue with array echo

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.

Upvotes: 0

Views: 78

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

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++;
}

Upvotes: 1

Neeraj Kumar
Neeraj Kumar

Reputation: 504

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!

Upvotes: 0

Related Questions