I have the following iterration:
foreach ($filelines as $indexrow => $line) {
$columns = explode($splitter, $line);
$row = [];
foreach ($columns as $columnindex => $column) {
$column = $this->prepareValue($column);
if (RegexValidation::isNum($column)) {
$row[] = $this->setValue('num', $column, $indexrow, $columnindex);
} else {
if (RegexValidation::isMMYY($column)) {
$row[] = $this->setValue("mmyy", $column, $indexrow, $columnindex);
} else {
if (RegexValidation::isZip($column)) {
$row[] = $this->setValue("zip", $column, $indexrow, $columnindex);
}
}
}
}
$dataset[$indexrow] = $row;
}
I need to fill $row in a specific sequence, first is "zip," then "mmyy," then "num," etc. If there is no field, fill it as empty.
How to do this in the existing loop? Now I have a solution sort result array, but It needs to loop again.
I would use a special key and sort by the key. And - there is an elseif.
foreach ($filelines as $indexrow => $line) {
$columns = explode($splitter, $line);
$row = [];
foreach ($columns as $columnindex => $column) {
$column = $this->prepareValue($column);
$counter = 0;
if (RegexValidation::isNum($column)) {
$row['C_'.$counter++] = $this->setValue('num', $column, $indexrow, $columnindex);
} elseif (RegexValidation::isMMYY($column)) {
$row['B_'.$counter++] = $this->setValue("mmyy", $column, $indexrow, $columnindex);
} elseif (RegexValidation::isZip($column)) {
$row['A_'.$counter++] = $this->setValue("zip", $column, $indexrow, $columnindex);
} else { // If there is no field, fill it as empty
$row['Z_'.$counter++] = '';
}
}
ksort($row);
$dataset[$indexrow] = array_values($row);
}
Use A for the group you want to have first, then B for the group you want next, and so on. If you want to preserve the order within the group, you should have leading zeros on the counter, because A_10 ist sorted before A_9. Change that to A_09, or A_000009 depending on the size of your dataset.
I am currently using PHPExcel to output an order's information to Excel.
I have the following foreach-statements that ultimate generate my data such as the city in which the order was made in, the restaurant name, etc.
Pardon the nested foreach-loops - it was the only way I could nest through all that many cities and legal entities and restaurants to generate the data the customer wanted.
Using an answer posted here which dynamically generates the row and column integer, I tried it in my code.
$row = 1; // 1-based index
$col = 0;
foreach ($this->data['total_by_city'] as $city_id => $total_city){
$city_name = '';
foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
$legal_entity_name = '';
foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
$orders = $this->data['order_by_restaurants'][$restaurant_id];
$restaurant_name = '';
if ($orders)
{
foreach ($orders as $order_id => $order)
{
$restaurant_name = $order['restaurant_name'];
$legal_entity_name = $order['legal_entity'];
$city_name = $order['city'];
echo $row . ", ". $col . "<br>";
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
$row++;
echo $row . ", ". $col . "<br>";
$col = 0;
}
}
}
}
}
When I ran my code, and attempted to open the .xlsx file, I received a "corrupted Excel spreadsheet" error in Excel.
To make sure that my rows and column (respectively) indices are correct, I printed them out:
1, 0
2, 8
2, 0
3, 8
3, 0
4, 8
4, 0
5, 8
5, 0
6, 8
6, 0
7, 8
7, 0
8, 8
8, 0
9, 8
9, 0
10, 8
10, 0
11, 8
11, 0
12, 8
12, 0
13, 8
13, 0
14, 8
14, 0
15, 8
15, 0
16, 8
From that observation, I see that the rows and columns indices I am using are incorrect. They are both in the wrong location and reset/incremented incorrectly.
My question is - how do I increment and reset my column and row indices correctly?
You have forgot to increment Column index I guess
try following
$row = 1; // 1-based index
$col = 0;
foreach ($this->data['total_by_city'] as $city_id => $total_city){
$city_name = '';
foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
$legal_entity_name = '';
foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
$orders = $this->data['order_by_restaurants'][$restaurant_id];
$restaurant_name = '';
if ($orders)
{
foreach ($orders as $order_id => $order)
{
$restaurant_name = $order['restaurant_name'];
$legal_entity_name = $order['legal_entity'];
$city_name = $order['city'];
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
$col++; // Increment for each Cell
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
}
$col=0;
$row++;
}
}
}
Note- in excel each Cell should have unique RowID and ColumnID
Using KCdod's answer, I got it to work:
$row = 1; // 1-based index
$col = 0;
foreach ($this->data['total_by_city'] as $city_id => $total_city){
$city_name = '';
foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
$legal_entity_name = '';
foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
$orders = $this->data['order_by_restaurants'][$restaurant_id];
$restaurant_name = '';
if ($orders)
{
foreach ($orders as $order_id => $order)
{
$restaurant_name = $order['restaurant_name'];
$legal_entity_name = $order['legal_entity'];
$city_name = $order['city'];
echo $row . ", ". $col . "<br>";
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
$row++;
echo $row . ", ". $col . "<br>";
$col = 0;
}
}
}
}
}
I am using from_array:
$arrayData = array(
array(NULL, 2010, 2011, 2012),
array('Q1', 12, 15, 21),
array('Q2', 56, 73, 86),
array('Q3', 52, 61, 69),
array('Q4', 30, 32, 0),);
$objPHPExcel->getActiveSheet()->fromArray(
$arrayData, // The data to set
NULL, // Array values with this value will not be set
'C3' ); // Top left coordinate of the worksheet range where we want to set these values (default is A1)
In a project with PHP, I assigned values in a multidimensional array, and this function works fine.
Thanks.
I would like to produce something like this in an excel file but it produces this which is not what i want. I would like the output to be exactly the same. If the number of tests are four, then four tests are dynamically printed vertically in a cell in excel file as in the word document plus the additional details
here is more details of the number of tests and their details.
$trd = $this->getRequestedTestsDisplay2($labref); //An array of objects 4 in total
$coa_details = $this->getAssayDissSummary($labref); //details as shown http://nqcl.alphybay.com/word.png
phpword code
for ($i = 0; $i < count($trd); $i++) {
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
}
$table3->addRow(400);
$table3->addCell(1500)->addText($trd[$i]->name);
$table3->addCell(1500)->addText($trd[$i]->methods);
$table3->addCell(1700)->addText($trd[$i]->compedia);
$table3->addCell(1800)->addText($trd[$i]->specification);
$table3->addCell(1700)->addText('DETERMINED', $style3);
$table3->addCell(1500)->addText($trd[$i]->complies);
}
phpxcel code
$row = 19;
$col=1;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
$worksheet
->setCellValueByColumnAndRow($col, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->complies);
$col++;
}
$row++;
}
Perhaps resetting column for each row, and not overwriting each value in the same row/column:
$row = 19;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
$col=1;
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
$worksheet
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->complies);
}
$row++;
}
EDIT
You mean something like:
$row = 19;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
$col=1;
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
}
$worksheet
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->complies);
}
$row++;
}
So I am attempting to loop through and print these values to an excel file. I was able to figure that out but am now attempting to add code to be able to explode the original values I was getting.
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
Everything works until here. The values we get come out as 111X222X333 so I want to explode them and then query a new array of values
list($sid,$gid,$qid) = explode("X", $value)
$results="select * from lime questions where sid =".$sid." and gid =".$gid." and qid".$qid;
}
foreach(mysql_fetch_assoc($results)){
these were the 3 lines I added to an already working loop. I am wondering what I am doing wrong here.
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $results);
$row++;
}
}
$row = 1;
$col++;
}
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}
Where am I messing up the loops
I am using this piece of code in PHP to query a database and import the data to an excel file. Currently I am getting the data from the database, but I can't get the headers.
Can anyone tell me how to get the headers from the database?
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$row = 1;
$col++;
}
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}