/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Optimization Help

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;
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ehimeauthorApr 20.2011 — //fixed

[code=php]
case "Ordination Credentials":

// hack -jd
include ('class.ezpdf.php');
$pdf =& new Cezpdf('LETTER','landscape');
$pdf->selectFont('./fonts/mtcorsva.afm');
$pdf->ezSetMargins(30,1,30,1);

extract($_GET);
if (!isset($in)) { $in = "28219"; }
if (!isset($out)) { $out = "28219"; }
$betweenspec = "BETWEEN $in AND $out";

$query = "
SELECT orders.orders_id,
orders_products_attributes.orders_products_id,
orders_products_attributes.products_options,
orders_products_attributes.products_options_values

FROM orders, orders_products, orders_products_attributes
WHERE orders.orders_id = orders_products.orders_id AND orders_products.orders_products_id = orders_products_attributes.orders_products_id

AND orders_products.products_id in (53,129,127,58,166,128, 338, 341)
AND orders.orders_id

$betweenspec
"; //END OF SQL QUERY

$result = mysql_query(trim($query)) or die("Could not run database query, please contact webmaster");
$count = 0; // count for bgcolor of each row

while ($query_data = mysql_fetch_array($result)) {
$orders_id = $query_data['orders_id'];
$orders_prod_id = $query_data['orders_products_id'];
$orders_option = $query_data['products_options'];
$orders_value = $query_data['products_options_values'];
$creds[$orders_prod_id][$orders_option] = $orders_value;
$creds[$orders_prod_id]["Order Number"] = $orders_id;
}

$started = false;

foreach( $creds as $orders_products_id=>$options ) {
if($started) { $pdf->ezNewPage(); } //create new page

$started = true;

if ($name == $lname) { $name = " "; }

if($options["Name on Credential"]){
$name = $options["Name on Credential"];
$lname = $name;
}

$day = $options["Ordination Day"];
$month = $options["Ordination Month"];
$year = $options["Ordination Year"];
$ordid = $options["Order Number"];
// END OF CREATE VARS

// create date
extract($_GET);
if (!isset($yname)) { $yname = "370"; }
if (!isset($ydate)) { $ydate = "287"; }

$pdf->ezSetY($yname); $pdf->ezText($name,28,array('left'=>-35, 'justification'=>'center')); //287,28,-35
$pdf->ezSetY($ydate); $pdf->ezText($day,16,array('left'=>-220, 'justification'=>'center')); //370,16,-220
$pdf->ezSetY($ydate); $pdf->ezText($month,16,array('left'=>40, 'justification'=>'center')); //370,16,40
$pdf->ezSetY($ydate); $pdf->ezText($year,16,array('left'=>375, 'justification'=>'center')); //370,16,375
$pdf->ezSetY(87); $pdf->ezText($ordid,8,array('left'=>510, 'justification'=>'center')); //87,8,510
}

$pdfcode = $pdf->output();
$dir = './tmp';

// save
if (!file_exists($dir)) {
mkdir ($dir,0777);
}

$fname = $dir.'/PDF_ordinations.pdf';
$fp = fopen($fname,'w');
fwrite($fp,$pdfcode);
fclose($fp);

header('Location:'.$fname);

break;
[/code]
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...