Hi Folks,
I'm have a bit of a problem when trying to generate a formatted flat file from database values.
What this script is doing is generating a flat file containing client billing information to be passed to my credit card processing program. At any given point, it may be doing this for up to 200 clients.
The way the script works is as follows:
A previous script generates a table containing the full list of clients that are to be billed on that particular day, each with their own checkbox (named "sel[CLIENT ID]"). When a user is checked and the form submitted, it runs this script.
Now, when I run this with a single client selected, it works beautifully. The client information gets passed correctly, formatted correctly, and written to the file correctly. However, when I run this with more than one client selected, it times out and I get ye olde "max_execution_time" error, even with just 2. I increased the "max_execution_time" from 30 to 60 seconds to no avail.
It doesn't seem to be the actual working with the file that is causing the problem - I commented that code out and still got the same result, so I'm assuming it has to do with the formatting of the billing variables. The credit card processor needs a very specific format (fixed-width), with certain portions of the file having a specified width, padded with spaces or a char (ie - "0"s).
As you can see, right now it is using while() loops to accomplish many of the formatting needs. This works, but I feel as if doing it another way could help the problem. What do you think?
Here's the code:
echo "Generating bill!<br><br>";
$cquery = "select * from [i]yadda yadda[/i]";
$cresult = mysql_query($cquery, $dbconnect);
$cnum = mysql_num_rows($cresult);
$recnum = 1;
$file = "/path/to/billingfile.txt";
$fp = fopen($file, "w");
for ($i=0; $i<$cnum; $i++) {
$cinfo = mysql_fetch_array($cresult);
$custbox = "sel".$cinfo["customer_id"];
if ($_REQUEST[$custbox] == "on") {
$bquery = "select * from billing where customer_id = \"".$cinfo["customer_id"]."\"";
$bresult = mysql_query($bquery, $dbconnect);
$binfo = mysql_fetch_array($bresult);
$dquery = "select domain_name from domain where customer_id = \"".$cinfo["customer_id"]."\"";
$dresult = mysql_query($dquery, $dbconnect);
$dinfo = mysql_fetch_array($dresult);
/****************************
Generate and fwrite billing
line for client.
****************************/
while (strlen($recnum) != 4) {
$recnum = "0".$recnum;
}
$recnum .= "N S";
$ccnum = $binfo["cc_num"];
$ccsplit = explode("~", $ccnum);
$ccnum = $ccsplit[0];
while (strlen($ccnum) != 21) {
$ccnum .= " ";
}
$ccexp = $ccsplit[1];
$amount = $binfo["amount"];
$amtsplit = explode(".", $amount);
$amount = $amtsplit[0].$amtsplit[1];
while (strlen($amount) != 12) {
$amount = "0".$amount;
}
if (preg_match("/(.{6}).*/", $dinfo["domain_name"], $match)) {
$client = " $match[1]";
}
$ccavs = $binfo["ccavs"];
$avssplit = explode("~", $ccavs);
$avs1 = $avssplit[0];
while (strlen($avs1) != 20) {
$avs1 .= " ";
}
$avs2 = " ".$avssplit[1];
while (strlen($avs2) != 10) {
$avs2 .= " ";
}
$spacepad1 = " ";
$spacepad2 = "";
while(strlen($spacepad2) != 56) {
$spacepad2 .= " ";
}
$spacepad3 = "";
while(strlen($spacepad3) != 151) {
$spacepad3 .= " ";
}
$billingline = $recnum.$ccnum.$spacepad1.$ccexp.$amount.$spacepad2.$client.$spacepad3
."00000000000000@".$avs1.$avs2."\n";
echo "$billingline<br>";
fwrite($fp, $billingline);
}
}
fclose($fp);
echo "<br> END";