Hi

I have a db that has the following Data for example:

PolicyNumber, StoreId, ConsultantName,ClientName, ClientSurname

I would like to have a report that will give me a list of policies done for that day seperated by store printed to PDF.

So Basically

Store - 12CEL
24Hr001 Bob Joe Soap
24Hr002 Bill Gill Henry

Store - 1156
24Hr003 Luke Manfred Kiy
24Hr004 Peter Ursula Jent

I currently use R&OS for pdf Printing
http://www.ros.co.nz/pdf

The Code below Gives me all policies, I just need to now seperate them in the PDF

//include pdf class
include ('class.ezpdf.php');

//Include PHP Mailer Class
require("class.phpmailer.php");

//Connect to database
mysql_connect("localhost", "root", "");
@mysql_select_db("mobility") or die("Unable to select database");

$queryP= mysql_query("SELECT * FROM tblpolicies WHERE CaptureDate='2006-04-03' ORDER BY StoreId");

$pdf =& new Cezpdf();
//$pdf->addJpegFromFile('policy.jpg',60,$pdf->y-750,500,0);
$pdf->selectFont('./fonts/Helvetica.afm');

$p = 750;


While($x=mysql_fetch_array($queryP)){ 

$pdf->addText(50,$p,7,"{$x['PolicyNumber']}");
$pdf->addText(80,$p,7,"{$x['StoreId']}");
$pdf->addText(120,$p,7,"{$x['NickName']}");
$pdf->addText(220,$p,7,"{$x['ClientsName']}");
$pdf->addText(330,$p,7,"{$x['DateReceived']}");
$pdf->addText(410,$p,7,"{$x['Comments']}");
$p = $p - 10;

if($p == 10){
    $pdf->ezNewPage();
    $p = 750;
    }



}

$pdf->ezText("\n\n\n\n\n\n\n" . $body,16,array('justification'=>'centre'));
$pdf->output();
$pdf->ezStream();

//write pdf stream to pdf file 
$pdfcode = $pdf->ezOutput();
$fp=fopen('policy.pdf','wb');
fwrite($fp,$pdfcode);
fclose($fp);

You need to save the store id (near the end of your While loop) then and compare each new one to the previous one at the start of the loop. When you get a new one, then use the ezNewPage()command to start a new page (and reset your line counter).

You need to save the store id (near the end of your While loop) then and compare each new one to the previous one at the start of the loop. When you get a new one, then use the ezNewPage()command to start a new page (and reset your line counter).

Many Thanks for the reply

I tried the following to test and give me a space between storeId's

While($x=mysql_fetch_array($queryP)){ 

$pdf->addText(50,$p,7,"{$x['PolicyNumber']}");
$pdf->addText(80,$p,7,"{$x['StoreId']}");
$pdf->addText(120,$p,7,"{$x['NickName']}");
$pdf->addText(220,$p,7,"{$x['ClientsName']}");
$pdf->addText(330,$p,7,"{$x['DateReceived']}");
$pdf->addText(410,$p,7,"{$x['Comments']}");
$p = $p - 10;

if($p == 10){
    $pdf->ezNewPage();
    $p = 750;
    }


if($x['StoreId'] != $StoreId){
$p = $p - 10;    
}

$StoreId = $x['StoreId'];
}

But it doesnt work properly and I cant figure out why

That won't do it!

1. If you are already at the end of a page, you will skip to a new page (line 12-15) THEN you check for a change in Store ID and potentially skip to a new page again.

2. On lines 18 - 20, you are checking for a change in Store ID and if it is different you are decrementing $p. At that point, you should be going to a new page. Alternately, you could make $p = 20 and let the page skip happen on the next cycle but that makes it more complicated that it needs to be.

What you need is something like:

$first_store = "yes";

While($x=mysql_fetch_array($queryP)){ 
 
   $pdf->addText(50,$p,7,"{$x['PolicyNumber']}");
   $pdf->addText(80,$p,7,"{$x['StoreId']}");
   $pdf->addText(120,$p,7,"{$x['NickName']}");
   $pdf->addText(220,$p,7,"{$x['ClientsName']}");
   $pdf->addText(330,$p,7,"{$x['DateReceived']}");
   $pdf->addText(410,$p,7,"{$x['Comments']}");
   $p = $p - 10;
 
   if ($first_store == "yes") {
      $StoreId = $x['StoreId'];
      $first_store = "no";
   }   

   elseif($x['StoreId'] != $StoreId OR $p == 10){
      $pdf->ezNewPage();
      $p = 750;
      $StoreId = $x['StoreId'];
   }
}

Hi

It skips 1 line as intended. My problem is that it doesnt seperate when the new store is in the list for some reason, I also tried using your method and it changes the page but again not when the store changes. I have attached both pdf's for you to see the results.

my code gives me this output:

68351 11CCC 24hrLelanieJooste M VAN JAARSVELD 2006-04-03 00:00:00

80253 11GEE 24HrZandile EN SIBISI 2006-04-01 00:00:00

80251 11GEE 24HrZandile KD MAKGATHO 2006-04-01 00:00:00
79681 11HGC 24hrLorna SE MABASO 2006-04-01 00:00:00 No CPD Attached

79682 11HGC 24HrGanief1 R MARCUS 2006-04-01 00:00:00
70744 11HGC 24HrAdeeba SETHOGA 2006-04-02 00:00:00
79683 11HGC 24HrAdeeba MS SITHOLE 2006-04-02 00:00:00
69931 12CEL 24HrThembi RR Makhafola 2006-04-01 00:00:00

I would like it to do this:

68351 11CCC 24hrLelanieJooste M VAN JAARSVELD 2006-04-03 00:00:00

80253 11GEE 24HrZandile EN SIBISI 2006-04-01 00:00:00
80251 11GEE 24HrZandile KD MAKGATHO 2006-04-01 00:00:00

79681 11HGC 24hrLorna SE MABASO 2006-04-01 00:00:00 No CPD Attached
79682 11HGC 24HrGanief1 R MARCUS 2006-04-01 00:00:00
70744 11HGC 24HrAdeeba SETHOGA 2006-04-02 00:00:00
79683 11HGC 24HrAdeeba MS SITHOLE 2006-04-02 00:00:00

69931 12CEL 24HrThembi RR Makhafola 2006-04-01 00:00:00

Now that I see the output, it's obvious. I didn't look at the whole thing carefully enough the first time. The While loop writes out the PDF output for a new store ID before it skips to a new page. The order needs to be changed as follows:

$first_store = "yes";
 
While($x=mysql_fetch_array($queryP)){ 

   if ($first_store == "yes") {
      $StoreId = $x['StoreId'];
      $first_store = "no";
   }   
 
   elseif($x['StoreId'] != $StoreId OR $p == 10){
      $pdf->ezNewPage();
      $p = 750;
      $StoreId = $x['StoreId'];
   }
 
   $pdf->addText(50,$p,7,"{$x['PolicyNumber']}");
   $pdf->addText(80,$p,7,"{$x['StoreId']}");
   $pdf->addText(120,$p,7,"{$x['NickName']}");
   $pdf->addText(220,$p,7,"{$x['ClientsName']}");
   $pdf->addText(330,$p,7,"{$x['DateReceived']}");
   $pdf->addText(410,$p,7,"{$x['Comments']}");
   $p = $p - 10;
 

}

I think that should work properly. Now it checks for a new store and skips to a new page before it creates any output for the new store.

Much better Thanks :)

It definately seperates the stores no, but I only want it to create a space between stores not go to a new page for every store. Also on the first page using ur code it prints at the bottom then skips to the next page.

I have attached the output

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.