I wrote out this code, but have noticed if I try to print orders that
are plus 1000~ I get a large hang in the program, can someone offer
some help in optimizing it?
[code=php]
case “Ordination Credentials”:
$q = “SELECT o.date_purchased, fd.template_fn, o.customers_name, op.orders_id, opa.orders_products_id, products_name, op.products_quantity, field_name, products_options_values
FROM orders_products AS op
join orders as o on o.orders_id = op.orders_id
JOIN fulfillment_doc_products AS fdp ON fdp.product_id = op.products_id
JOIN fulfillment_docs AS fd ON fd.doc_id = fdp.doc_id
JOIN orders_products_attributes AS opa ON opa.orders_products_id = op.orders_products_id
right JOIN fulfillment_doc_fields AS fdf ON fdf.products_options_name = opa.products_options
AND fdf.doc_products_id = fdp.doc_products_id
WHERE op.orders_id
BETWEEN $in
AND $out
AND fd.doc_id = $doc_id
ORDER BY o.orders_id, products_name, opa.orders_products_id, field_name, products_options_values”;
$r = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)) {
//Template Filename is managed on admin front end
$template_fn = getcwd().’/templates/’ . $row[‘template_fn’];
//Standard db output filtering and processing into multi-dimensional array as seen above
$datas[$row[‘orders_products_id’]][$row[‘field_name’]] = sanitize_db_output($row[‘products_options_values’]);
$datas[$row[‘orders_products_id’]][‘order_number’] = $row[‘orders_id’];
$datas[$row[‘orders_products_id’]][‘products_quantity’] = $row[‘products_quantity’];
}
//pdftk allows us to stuff things into custom fields in pdf files
require(‘pdftk-php/pdftk-php.php’);
//create new pdftk object
$pdfmaker = new pdftk_php;
//init stuff
$fdf_data_names = array();
$fields_hidden = array();
$fields_readonly = array();
//go through array now
foreach($datas as $data) {
//multiple qty gets multiple credentials
for ($i = 1; $i <= $data[‘products_quantity’]; $i++) {
//day must look like 1st, 2nd, 3rd etc..
$data[‘date-day’] = ordinalize($data[‘date-day’]);
//compiling pdf
$pdf_content = $pdfmaker->make_pdf($data, $fdf_data_names, $fields_hidden, $fields_readonly, $template_fn);
//report errors, show debug info
if(empty($pdf_content)) {
echo “pdf empty ” . $template_fn;
echo “<br><pre>” . print_r($data) . “</pre>”;
die();
}
//write data to temp file with prefix+random characters filename
$pdf_filename = tempnam(‘/tmp/’, ‘ulc-fulfillment’);
$handle = fopen($pdf_filename, “w”);
fwrite($handle, $pdf_content);
fclose($handle);
//add filename to array for combination
$pdfs[] = $pdf_filename;
}
}
//combine all temp pdfs in array
$final_pdf = $pdfmaker->combine_pdf($pdfs);
//clean up temp files
foreach($pdfs as $pdf) {
if (file_exists($pdf)) { unlink($pdf); }
}
//send to browser
header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=ordination-credentials-$datestamp.pdf”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
echo $final_pdf;
break;