TCPDF and repeating values

I want to produce a pdf document using mysql and TCPDF.

I can do this OK in some circumstances but I now have a problem. I have two tables - documents and parties - they are linked by a common field docid.

I have for table documents, the fields:
docid, doctitle, idcode

and for the parties table:
partyid, docid, party (the field party is the name of the party)

For any document there could be any number of parties and for a single idcode there could be any number of documents.

For example, for idcode = 12345 there might be 2 documents called doctitle 1 and doctitle 2. For doctitle 1 there are 3 parties to it party1, party 2 and party 3. For doctitle 2 there are two parties - party4 and party 5.

For any idcode (which is passed through by the url) I want the pdf to give a list such as:

Doctitle 1:
party name 1
party name 2
party name 3

Doctitle 2:
party name 4
party name 5

Part of my code is:

mysql_select_db($database_process, $process);
$result2 = mysql_query("SELECT parties.docid, GROUP_CONCAT(party SEPARATOR ‘<br>’) AS partylist, documents.doctitle
FROM parties INNER JOIN documents ON parties.docid=documents.docid
WHERE parties.idcode = ‘$appidpassed’
GROUP BY parties.docid
LIMIT 1
") or die(mysql_error());

while($row = mysql_fetch_array($result2))
{
$doctitle = $row[‘doctitle’];
$parties = $row[‘partylist’];

}

and then further down the page:

$doctitle<br>
$parties

This only gives one document and the parties associated with it. I know the solution is probably easy but I can’t see it.

Does anyone have any ideas?

“LIMIT 1”… now… what could that possibly do…

OK - I took that out and now just get the second document title and parties instead of the full list.

"while($row = mysql_fetch_array($result2))
{
$doctitle = $row[‘doctitle’];
$parties = $row[‘partylist’];

}

and then further down the page:

$doctitle<br>
$parties"

So… you walk through the whole loop, and at the end, when it’s finished processing, you output whatever is stored in your variables.

Do you see the logic flaw in there?

I can yes but I just don’t know how to solve it! The bulk of the pdf generating part of my code is below (with some text removed to make it shorter) - I don’t know how to get the two bits in the correct places and in the correct format!

require_once(‘…/tcpdf/config/lang/eng.php’);
require_once(‘…/tcpdf/tcpdf.php’);

class MYPDF extends TCPDF {

// Page footer
public function Footer() {
    // Position at 15 mm from bottom
    $this-&gt;SetY(-15);
    // Set font
    $this-&gt;SetFont('dejavusans', 'I', 6);
    // Page number
    $this-&gt;Cell(0, 10, 'Clear Appointments Ltd registered office 145-157 St John Street, London, EC1V 4PW, England. Registered No: 07512375', 0, false, 'L', 0, '', 0, false, 'T', 'M');
}

}

if(isset($_GET[‘idcode’])){
$appidpassed = $_GET[‘idcode’];
}else{
$appidpassed = 4; // set this to your default value if no URL value is present
}

// get data from table

mysql_select_db($database_process, $process);
$result2 = mysql_query("SELECT parties.docid, GROUP_CONCAT(party SEPARATOR ‘<br>’) AS partylist, documents.doctitle
FROM parties INNER JOIN documents ON parties.docid=documents.docid
WHERE parties.idcode = ‘$appidpassed’
GROUP BY parties.docid
") or die(mysql_error());

while($row = mysql_fetch_array($result2))
{
$doctitle = $row[‘doctitle’];
$parties = $row[‘partylist’];

}

// create new PDF document
$pdf = new myPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, ‘UTF-8’, false);

$pdf->SetPrintHeader(false);
// $pdf->SetPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont(‘dejavusans’, ‘’, 10);

// add a page
$pdf->AddPage();
// create some HTML content
// set JPEG quality
$pdf->setJPEGQuality(100);

$pdf->Image(‘logo.jpg’, 70, 5, 73, 20, ‘’, ‘’, ‘’, true, 100);

$htmlcontent = "<html>

$doctitle<br>
$parties

			  "; 				

// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0, ‘’);

//Close and output PDF document
$directory = ‘…/docs/’;
$docname = ‘Confirmation_Letter_’ . $appidpassed . ‘.pdf’;
$path=$directory.$docname;
$pdf->Output($path, ‘F’);


$htmlcontent = "";
while($row = mysql_fetch_array($result2)) 
{ 
$htmlcontent .= $row['doctitle']."<br />".$row['partylist']."<br /><br />";
}

and erase the $htmlcontent stuff lower in the page.

Thank you - that certainly gives me an output I wanted.

I also wanted to get the data into the middle of some other document that I want to generate. So for example, the pdf I output could look like:

pdf start:

Here is a list of the documents and the parties linked to the documents:

Doctitle 1:
party name 1
party name 2
party name 3

Doctitle 2:
party name 4
party name 5

If you would like to speak to me about any of this please contact me on 020 7090 12345.

pdf end

I tried this by sandwiching the list code between two other htmlcontents but it doesn’t work.

What format should I be using?

Sorry to be a pain but I am learning (very slowly).

“sandwiching the list code”… you mean copying the while loop? or putting $htmlcontent in there?

Like this:

$htmlcontent = "<html>
Here is the first bit of text I would like printed.
";

while($row = mysql_fetch_array($result2))
{
$htmlcontent .= $row[‘doctitle’].“<br />”.$row[‘partylist’].“<br /><br />”;
}

$htmlcontent = "<html>
Here is the final paragraph.
";

Look reaaaaaaaal closely…


$htmlcontent = "<html> 
Here is the first bit of text I would like printed.
";

while($row = mysql_fetch_array($result2)) 
{ 
$htmlcontent [B][COLOR="Red"].[/COLOR][/B]= $row['doctitle']."<br />".$row['partylist']."<br /><br />";
} 

$htmlcontent = "<html> 

.= translates as “Append this to my variable…” ($var .= $somethingelse is equivilant to $var = $var.$somethingelse )

Excellent - it all works AND I now understand.
Very many thanks.