What I would do is: 1. Create a 'template file'... something like: __TODAY__ Invoice #: __INVOICE_NUMBER__ Amount: __INV_AMT__ (__CURRENCY__) etc... etc... etc... 2. Then in your Invoice Processing routine, read the whole template file into a string. $template = file_get_contents ($template_file_name); // file_get_contents is a standard PHP function. 3. For each invoice: $invoices = db_get_invoices ($todays_date); // Your db routine should return an array of invoice // numbers. I'm assuming that invoices are generated // daily, hence the use of 'todays_date' a variable // containing todays date... in whatever format you // prefer to use. You might even consider making the // routine NOT require the date, and defaulting // to todays date, if none is passed. foreach ($invoices as &$invoice_number) { $text = ''; $isok = format_invoice ($template, $invoice_number, $text); if ( $isok ) { $isok = output_invoice ($text); if ( ! $isok ) { // handle errors appropiately... // write to log file or something. } } else { // handle errors appropiately... // write to log file or something. } } // The function format_invoice should be written in // such a manner that it looks up all the necessary // information from the database and performs multiple // str_replaces... until all dynamic fields in the // template have been replaced with actual values. // The WHOLE thing is then returned to the calling // routine where it is handled appropriately. // The function output_invoice might then append the // contents (e.g. the invoice) to a file, which is // then later printed... and then the invoices // snail mailed to customers... it all depends on what // you intend on doing with format_invoice might look something like this: format_invoice ($template, $invoice_number, &$text) { $isok = true; $isok = db_lookup_invoice ($invoice_number, $invoice_values); // db_lookup_invoice returns an array of ALL the values // associated with a particular invoice number if ( $isok ) { $rtn_str = $template; $rtn_str = str_replace ('__TODAY__', $todays_date, $rtn_str); $rtn_str = str_replace ('__INVOICE_NUMBER__', $invoice_number, $rtn_str); $rtn_str = str_replace ('__INV_AMT__', $invoice_values['amount'], $rtn_str); $rtn_str = str_replate ('__CURRENCY__', $invoice_values['currency'], $rtn_str); . . . $text = $rtn_str; // put the rtn_str into $text where // the calling routine will have // access to it. } return $isok; // return a success status to calling routine // tells the calling routine whether THIS routine // succeeded or not. }