|
Issue with arrays and Spreadsheets in PEAR
Hello,
I have an XDP (XML) form that I'm dumping values from into a PHP file that interprets these values and places them in a spreadsheet using PHP PEAR's Spreadsheet Excel Writer.
A-U are a lot of values that will be calculated into three different sums between Y-ZZ. Ysum-ZZsum are the sums of all Y-ZZ values. It's a lot like an excel sheet.
So when the values are dropped into the php script, this is what it looks like:
PHP Code:
$_FIELD=array(
"foreman" => $_POST{'foreman'},
"employee" => $_POST{'employee'},
"curdate" => $_POST{'mon'},
"A" => $_POST{'A'},
"B" => $_POST{'B'},
"C" => $_POST{'C'},
"D" => $_POST{'D'},
"E" => $_POST{'E'},
"F" => $_POST{'F'},
"G" => $_POST{'G'},
"H" => $_POST{'H'},
"I" => $_POST{'I'},
"J" => $_POST{'J'},
"K" => $_POST{'K'},
"L" => $_POST{'L'},
"M" => $_POST{'M'},
"N" => $_POST{'N'},
"O" => $_POST{'O'},
"P" => $_POST{'P'},
"Q" => $_POST{'Q'},
"R" => $_POST{'R'},
"S" => $_POST{'S'},
"T" => $_POST{'T'},
"U" => $_POST{'U'},
"jobpick" => $_POST{'jobpick'},
"phasepick" => $_POST{'phasepick'},
"costpick" => $_POST{'costpick'},
"Y" => $_POST{'Y'},
"Z" => $_POST{'Z'},
"ZZ" => $_POST{'ZZ'},
"Ysum" => $_POST{'Ysum'},
"Zsum" => $_POST{'Zsum'},
"ZZsum" => $_POST{'ZZsum'},
);
$foreman = $_POST['foreman'];
$employee = $_POST['employee'];
$curdate = $_POST['mon'];
$A = $_POST['A'];
$B = $_POST['B'];
$C = $_POST['C'];
$D = $_POST['D'];
$E = $_POST['E'];
$F = $_POST['F'];
$G = $_POST['G'];
$H = $_POST['H'];
$I = $_POST['I'];
$J = $_POST['J'];
$K = $_POST['K'];
$L = $_POST['L'];
$M = $_POST['M'];
$N = $_POST['N'];
$O = $_POST['O'];
$P = $_POST['P'];
$Q = $_POST['Q'];
$R = $_POST['R'];
$S = $_POST['S'];
$T = $_POST['T'];
$U = $_POST['U'];
$jobpick = $_POST['jobpick'];
$phasepick = $_POST['phasepick'];
$costpick = $_POST['costpick'];
$Y = $_POST['Y'];
$Z = $_POST['Z'];
$ZZ = $_POST['ZZ'];
$Ysum = $_POST['Ysum'];
$Zsum = $_POST['Zsum'];
$ZZsum = $_POST['ZZsum'];
$filestring = $foreman . "-" . $curdate . ".dif";
$filestring = str_replace( ' ', '', $filestring );
$filestring = str_replace( ',', '', $filestring );
$co = "1";
$glacct = "503";
$daycode = "";
$rate = "30";
$amount = "1200";
$type = "0";
$union = "777";
$craft = "ELEC";
$state = "15";
$local = "151100";
$wc = "1560";
As you can see, the last dozen or so are concrete values, not true arrays, just variables.
The rest are arrays that dump multiple values depending on how many rows are inserted into this form.
Now, in order to structure it into the excel file (.dif in this case), this is the code I'm using.
PHP Code:
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer("$filestring");
$worksheet =& $workbook->addWorksheet($employee);
// The first row of the spreadsheet has hard values, no variability.
$worksheet->write(0, 0, 'CO'); //write(row, column, 'value')
$worksheet->write(0, 1, 'EMP');
$worksheet->write(0, 2, 'JOB');
$worksheet->write(0, 3, 'PHASE');
$worksheet->write(0, 4, 'COST');
$worksheet->write(0, 5, 'GL-ACCT');
$worksheet->write(0, 6, 'DAYCODE');
$worksheet->write(0, 7, 'HOURS');
$worksheet->write(0, 8, 'RATE');
$worksheet->write(0, 9, 'AMOUNT');
$worksheet->write(0, 10, 'TYPE');
$worksheet->write(0, 11, 'UNION');
$worksheet->write(0, 12, 'CRAFT');
$worksheet->write(0, 13, 'STATE');
$worksheet->write(0, 14, 'LOCAL');
$worksheet->write(0, 15, 'WC');
$worksheet->write(0, 16, 'DAY1');
$worksheet->write(0, 17, 'DAY2');
$worksheet->write(0, 18, 'DAY3');
$worksheet->write(0, 19, 'DAY4');
$worksheet->write(0, 20, 'DAY5');
$worksheet->write(0, 21, 'DAY6');
$worksheet->write(0, 22, 'DAY7');
$int=1; //This is to generate new rows per loop
while ($Y != "")
{
$worksheet->write($int, 0, $county);
$worksheet->write($int, 1, $employee);
$worksheet->write($int, 2, $jobpick);
$worksheet->write($int, 3, $phasepick);
$worksheet->write($int, 4, $costpick);
$worksheet->write($int, 5, $glacct);
$worksheet->write($int, 6, $daycode);
$worksheet->write($int, 7, $Y);
$worksheet->write($int, 8, $rate);
$worksheet->write($int, 9, $amount);
$worksheet->write($int, 10, $type);
$worksheet->write($int, 11, $union);
$worksheet->write($int, 12, $craft);
$worksheet->write($int, 13, $state);
$worksheet->write($int, 14, $local);
$worksheet->write($int, 15, $wc);
$worksheet->write($int, 16, $A);
$worksheet->write($int, 17, $D);
$worksheet->write($int, 18, $G);
$worksheet->write($int, 19, $J);
$worksheet->write($int, 20, $M);
$worksheet->write($int, 21, $P);
$worksheet->write($int, 22, $S);
$int++;
}
$workbook->close();
Doing this was creating, what I assume to be, an infinite loop. I've tried replacing the while with an if and it worked, but only ended up putting the last returned values in second (1) row. It didn't increment up nor make any other rows.
I've tried using the following variables in tons of different ways to make this work
PHP Code:
$county = count($Y); //I'm thinking this will tell it when to stop looping if that's necessary.
$ind=0; //used to indicate which value of $Y etc. to use per calculation, not to be confused with $int which has to start at one because of the hard values in row 1.
I hope this post wasn't too lengthy. There's some structuring I just haven't nailed correctly and I'm looking for some insights. I think I have all the tools here, they're just not being arranged right.
Thank you.
|