FPDF placing multiCells next to each other and go to a new line when end of line is reached

I’ve been working on this FPDF document for a year now. It works beautifully except for the last portion.
I have the last page as a mini photo gallery and I’ve used MultiCell to place the date, caption & image into a cell which is dynamically filled from MySql database. My problem is that when there are more than 5 photos, the cells go off the page to the right instead of moving to a new line.

I’ve tried placing the MultiCells on a new line for each photo, but when the end of the page is reached, the image is at the bottom of the previous page while the date and caption are on the next page.

Here is the code:

$sqld = "SELECT id, image, thumb, image_type, caption, site_no, img_date, rate FROM images WHERE site_no=$site AND rate>0 ORDER BY img_date";
$resultd = mysqli_query($dbcon, $sqld) or die(mysqli_error());

$pdf->AddPage();
$pdf->SetFont('Arial','B',11);
$pdf->SetFillColor(225,225,225);
$pdf->Cell(270,6,'E. Photo Gallery:',1,1,'L',1);
$pdf->Ln(2);
$pdf->SetFont('Arial', 'I', 9);
while($runImage = $resultd->fetch_assoc()) {
if(empty($image)){
}else{
{
$array[] =$runImage;
$image_height = 36;
$image_width = 48;
$image_height2 = 6;
$image_width2 = 48;
//get current X and Y
$start_x = $pdf->GetX();
$start_y = $pdf->GetY();
$pdf->SetFillColor(255,255,255);
// place image and move cursor to proper place. "+ 2" added for buffer
$pdf->MultiCell(48, 3, $runImage['img_date'] . ' - ' . $runImage['caption'] . $pdf->Image('uploads/thumbs/'.$runImage['image'],$pdf->GetX(), $pdf->GetY(),$image_width,$image_height, 'jpg'), 0, 1, 'L');
$pdf->SetXY($start_x + $image_width + 2, $start_y);
} // End of Gallery else Statement
} // End of Gallery foreach Statement
$pdf->SetFillColor(225,225,225);
} //-------------- End of If(Image) Gallery ---------------

Please can someone help me?
Thank you

Wouldn’t it be easiest to manually add the height to $start_y when you calculate that you’ve put as many across the page as it will handle? I haven’t used fpdf (other than a brief look to see if I could port it to another environment) so am not that familiar with how it should work. I would just think that checking if $start_x + $image_width + 2 is greater than the width, you would reset $start_x to zero (or 1, or whatever LHS is) and add $image_height to $start_y before you call SetXY().

1 Like

I’ll try that

Oh, you’re a genius, droopsnoot, Thank you very, very much :grinning:

For others that come across this post with the same problem, here is the code that I’ve tidied up and the changes:

$pdf->AddPage();
$pdf->SetFont('Arial','B',11);
$pdf->Cell(270,6,'I. Photo Gallery:',1,1,'L',1);
$pdf->Ln(2);
$pdf->SetFont('Arial', 'I', 9);
$pdf->SetFillColor(255,255,255);
$sqlc = "SELECT id, image, thumb, caption, site_no, rate, img_date FROM images WHERE site_no=$site ORDER BY img_date";
$resultc = $dbcon->query($sqlc);
$rowc = mysqli_fetch_array($resultc);
foreach($resultc as $runImage)
{
$array[] =$runImage;

$image_height = 36;
$image_width = 48;

//get current X and Y
$start_x = $pdf->GetX();
$start_y = $pdf->GetY();

if(empty($image)){
}else{
  
if($start_x > 240) {
  $pdf->Ln(40);
}

// place image and move cursor to proper place. "+ 2" added for buffer
$pdf->MultiCell(48, 3, $runImage['img_date'] . ' - ' . $runImage['caption'] . $pdf->Image('uploads/thumbs/'.$runImage['image'],$pdf->GetX(), $pdf->GetY(),$image_width,$image_height, 'jpg'), 0, 1, 'L');

$pdf->SetXY($start_x + $image_width + 2, $start_y);
} // End of Gallery else Statement
} // End of Gallery foreach Statement

Once again, thank you very much.

I’m glad it’s working, though I’m a bit confused at how it works. I assume Ln() throws it down by the number of pixels in the parameter, but doesn’t SetXY() just throw it back up to $start_y again? And surely $start_x will need to be reset if you move down to the next line?

Yes, I totally agree with you. It’s very confusing as to how the XY positions are achieved in the programming and after thinking about it for 2 hours, this is the only solution I could come up with following your advise and it worked. A few other attempts didn’t work and I was really skeptical about using an ‘if’ statement because it didn’t work on other attempts elsewhere in the document. But I’m oh, so happy that it has worked.

Well, that’s the main thing in the end. If it works now, leave it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.