Search code examples
phpfpdf

Show each data in different boxes using FPDF


I am working on fpdf, I want to created boxes and show each data from database in differnt boxes. Like if I have 2 boxes with different XY dimension so I want to show value 1 in first box and value 2 in second box, But issue is when I used my code it is showing both value 1 and 2 in both boxes. My code is

$w = array(82,95); //for XY dimension
for($i=0;$i<2;$i++)
{
  $reusult1 = $GLOBALS['conn']->query($sql2);
  $queryresult=mysqli_num_rows($reusult1);
  $this->Rect($w[$i], 47.5, 13, 9);

  $this->SetXY($w[$i] , 47.5);
  $this->SetFont( "Arial", "", 9);

  while($rows = mysqli_fetch_assoc($reusult1)){

    $this->Cell(4,4,$rows['position'],1,0,'C');
 }
}

in $rows['position'] have values 1 and 2.


Solution

  • Your logic was looping through both of the XY positions twice AND through the results twice. I don't have your data to test this with but it should solve your problem.

    $reusult1 = $GLOBALS['conn']->query($sql2);
    $w        = array(82,95); //for XY dimension
    for($i=0; $i < 2; $i++) {
    
      $this->Rect($w[$i], 47.5, 13, 9);
      $this->SetXY($w[$i] , 47.5);
      $this->SetFont( "Arial", "", 9);
      $data = mysqli_fetch_assoc($reusult1);
      $this->Cell(4,4,$data['position'],1,0,'C');
    
    }  // end of for loop