Solved

multi columns with fpdf

Posted on 2009-03-31
2
5,024 Views
Last Modified: 2012-08-14
Hi

been trying to figure the code below out it uses fpdf from here http://www.fpdf.org/
to convert a php page to pdf


at the minute it gets the results from the database and puts it into two columns like this below

info 1            info 7
info 2            info 8
info 3            info 9
info 4            info 10
info 5            info 11
info 6            info 12
                      info 13
                      info 14
                      info 15

how would i get it to go into a third column when it gets another 6 results




$pdf->SetXY(100,150);
$y_axis = 200;
$y2_axis = 0;
 
$sql = "select listingsdbelements_field_value from default_en_listingsdbelements where listingsdbelements_field_name = 'home_features' and listingsdb_id = '$listingID' limit 1"; 
$res = mysql_query($sql, $connection) or die(mysql_error());
 
//initialize counter
$i = 0;
 
while ($row = mysql_fetch_array($res)) {
$feature_index_list = explode("||", $row[0]);
sort($feature_index_list);
foreach($feature_index_list as $feature_list_item)
 
// prints 6 results then goes to the next cell
if ($i < 6) {
	$pdf->SetY($y_axis);
	$pdf->SetX(90);
 
	$pdf->Cell(20,4,$feature_list_item,0,1,L,'');
 
	//Go to next row
 
	$y_axis = $y_axis + 3;
	$i = $i+1;
	}
 
 
	else {
	$inity_axis = 200;
	$pdf->SetY($inity_axis+$y2_axis);
	$pdf->SetX(135);
 
 
 
	$pdf->Cell(20,4,$feature_list_item,0,1,L,'');
	$y2_axis = $y2_axis + 3;
 
	$i = $i+1;
 
 
 
	}
 
}

Open in new window

0
Comment
Question by:peter2001
2 Comments
 
LVL 10

Accepted Solution

by:
ollyatstithians earned 250 total points
ID: 24027172
Solution 1:
Have a look in the FPDF tutorials at the multi-column tutorial (http://www.fpdf.org/en/tutorial/tuto4.htm).
This is for a two columns, but we want 3, so to add a third one we need to alter the code a little. First we need to stop it making a new page after column 2. Alter the AcceptPageBreak() method by changing the line
  if($this->col<2)
to
  if($this->col<3)

Now we need to adjust the column widths and positions. Look at the method SetCol(). Change the line
  $x=10+$col*65;
to
  $x=10+$col*43;
That fixes the positions, but the widths are still adrift. This is set in ChapterBody(). Change this line
  $this->MultiCell(60,5,$txt);
to
  $this->MultiCell(40,5,$txt);

You should now have a fluid 3 column layout.

Solution 2:
Your code only detects if the list is less than 7 long (0 to 6). It then fails to column 2. We need to add a condition for column 3. Before your else block insert an elseif:

       elseif($i<12) {
        $inity_axis = 200;
        $pdf->SetY($inity_axis+$y2_axis);
        $pdf->SetX(202);     // Change the value here to adjust the position of col 3
        $pdf->Cell(20,4,$feature_list_item,0,1,L,'');
        $y2_axis = $y2_axis + 3;
        $i = $i+1;
       }

Repeat for each subsequent column.

Olly.
0
 
LVL 1

Author Comment

by:peter2001
ID: 24027436
Thats perfect that helps me understand it and your solution 2 works just as i need it to.

Thank you very much for your help

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question