How to export data in excel dynamically using PHPSpreadsheet/PHPExcel in PHP? - php

I have some data which I am trying to export in an excel sheet with help of PHP, data is dynamic so I am facing difficulties while exporting them. I have tried both in PHPExcel (which is deprecated) and in PHPSpreadsheet too. I have tried lots of solutions given in the community but no help!
How to print data dynamically in excel using PHP?
Data needs to print from column number = 36 (AK) and Row = 2
Data that needs to export:
MongoDB\Model\BSONDocument Object
(
[storage:ArrayObject:private] => Array
(
[dd1] => PWC>Yes,Deloitte>Yes,Media Type>Category A,Coverage Type>Quote,Service Line>TAX,Score>5
[dd2] => Service Line>Personal tax
)
)
though [dd1] contains labels and values too, like PWC[this is lable] >Yes [this is value], that's why I explode label and value separately.
Also, I have printed the labels in the first row already that's why I explode the value of the below code and also made a check if ($temp[0] == $labels[$i]) then only print value.
Completed Code:
$cols = 36;
for ($i = 0; $i < count($labels); $i++) {
if ($result["qualification"]["dd1"] != "") {
$found = false;
for ($j = 0; $j < count($dd1); $j++) {
$temp = explode(">", $dd1[$j]);
//need to check the weather labels of the header and these labels are the same? then only print
if ($temp[0] == $labels[$i]) {
$name = explode(">", $dd1[$j]);
$sheet->setCellValueByColumnAndRow($cols+1, $j+1, $name[1]);
$found = true;
break;
}
}
if (!$found)
$sheet->setCellValueByColumnAndRow($cols+1, $j+1 , "");
} else
$sheet->setCellValueByColumnAndRow($cols+1 , $j+1 , "");
if ($result["qualification"]["dd2"] != "") {
$found = false;
for ($j = 0; $j < count($dd2); $j++) {
$temp = explode(">", $dd2[$j]);
if ($temp[0] == $labels[$i]) {
$name = explode(">", $dd2[$j]);
$sheet->setCellValueByColumnAndRow( $cols, $j + 2, $name[1] );
$found = true;
break;
}
}
if (!$found)
$sheet->setCellValueByColumnAndRow($cols, $j+1 , "");
} else {
$sheet->setCellValueByColumnAndRow($cols , $j+1 , "");
}
$cols = $cols + 2;
}
I have tried lots of methods but no help but I made something in the HTML table which is working fine, unfortunately, I do not need an HTML table to excel because it has a lot of format issues and cons.
for reference only-
Here is the same working snippet in HTML Table:
$inn_table = "";
if($result['qualification']['dd1']!="") {
$dd1 = explode(",",$result['qualification']['dd1']);
}
if($result['qualification']['dd2']!=""){
$dd2 = explode(",",$result['qualification']['dd2']);
}
for($i=0;$i<count($labels);$i++) {
if($result['qualification']['dd1']!="") {
$found=false;
for($j=0;$j<count($dd1);$j++) {
$temp = explode(">",$dd1[$j]);
if($temp[0]==$labels[$i]) {
$name = explode(">",$dd1[$j]);
$inn_table .= '<td>'.$name[1].'</td>';
$found=true;
break;
}
}
if(!$found)
$inn_table .= "<td> </td>";
}
else
$inn_table .= "<td> </td>";
if($result['qualification']['dd2']!="") {
$found=false;
// echo '<pre>ass';print_r($dd2);
for($j=0;$j<count($dd2);$j++) {
$temp = explode(">",$dd2[$j]);
if($temp[0]==$labels[$i]) {
$name = explode(">",$dd2[$j]);
$inn_table .= '<td>'.$name[1].'</td>';
$found=true;
break;
}
}
if(!$found)
$inn_table .= '<td> </td>';
}
else{
//if(count($dd2Array)>0){
$inn_table .= '<td> </td>';
//}
}
} //end of for($i=0;$i<count($labels);$i++)
echo $inn_table;
echo "</tr>";

Related

PHPExcel: Unable to Print Dynamic Row and Column in Php?

Hi while exporting Dynmaic row and Column only columns exported not row, giving a Cannot modify header information - headers already sent by (output started at /var/www/html/excel/excelv1.php:615) in /var/www/html/excel/excelv1.php on line 625 error to find out
Error - image
Here is my code
For Dynamic Column code its working fine
$out_table = '';
$k = 0;
mysql_data_seek($query_result1, 0);
$col = 36;
while ($row1 = mysql_fetch_array($query_result1, MYSQL_ASSOC)) {
$sheet->setCellValueByColumnAndRow($col, 1, trim($labels[$k] . " (value1)", ','));
$sheet->setCellValueByColumnAndRow($col + 1, 1, trim($labels[$k] . " (value2)", ','));
$col = $col + 2;
$k++;
}
echo $out_table;
Here is Dynamic Row Code Which is not Working
$inn_table = "";
if($result['qualification']['dd1']!="") {
$dd1 = explode(",",$result['qualification']['dd1']);
}
if($result['qualification']['dd2']!=""){
$dd2 = explode(",",$result['qualification']['dd2']);
}
for($i=0;$i<count($labels);$i++) {
if($result['qualification']['dd1']!="") {
$found=false;
for($j=0;$j<count($dd1);$j++) {
$temp = explode(">",$dd1[$j]);
if($temp[0]==$labels[$i]) {
$name = explode(">",$dd1[$j]);
$inn_table .= '<td>'.$name[1].'</td>';
$found=true;
break;
}
}
}

PHPExcel: Dynamic Row and Column incrementing is not working for Array in phpexcel?

While printing column and row from an array it's showing blank not exporting any data
$inn_table = "";
if ($result['qualification']['dd1'] != "") {
$dd1 = explode(",", $result['qualification']['dd1']);
}
if ($result['qualification']['dd2'] != "") {
$dd2 = explode(",", $result['qualification']['dd2']);
}
for ($i = 0; $i < count($labels); $i++) {
if ($result['qualification']['dd1'] != "") {
echo '<pre>First';
print_r($dd1); ** //echo arrays**
for ($j = 0; $j < count($dd1); $j++) {
$temp = explode(">", $dd1[$j]);
if ($temp[0] == $labels[$i]) {
$name = explode(">", $dd1[$j]);
echo '<pre>First ';
print_r($name[1]); ** //echo names**
}
}
}
}
Here is my Array structure
and Here names which i am trying to export from Array
So i only trying to export names from that array please help
foreach($labels as $ind => $label) {
$index = $ind + 2;
$letter = range('A', 'Z')[$ind2];
$val = explode('>', $data);
$objPHPExcel->getActiveSheet()->setCellValue($letter . $index, $val[1]);
}

Restrict Search Domain

I have a data set that I'm searching for certain IPA symbols. I'd like to restrict the search domain in accordance with the value of $where shown in the code below, but don't know how. EDIT: Where $where is "onset" "nucleui" and "coda." Does anyone know how to restrict the search domain? (The code below is in php but the file is linked to an HTML file that a person could use to search for the IPA symbols in the data set.) EDIT: See added code at bottom.
//set up variables
$words = $table[0]; //row 1 of table
$target = $table[1]; //row 2
$indices = array(); //used to store column numbers
$IPAstr = $_POST["ipa"];
$where = $_POST["where"];
//Find $IPAstr in $target
for($i=1; $i<count($target); $i++)
{
if (mb_strpos($target[$i],$IPAstr) !== false)
$indices[] = $i;
}
//List realizations & count frequency
for($i=0; $i<count($indices); $i++)
{
$index = $indices[$i];
$ipalist = array();
echo "<table border=1><tr><td>".$target[$index]." in " . $words[$index]."</td></tr><td>";
//output each speaker and count frequency
for ($j=2; $j<count($table); $j++) {
echo ($j-1).": ".$table[$j][$index]."<br>";
$ipalist[$table[$j][$index]]++;
}
echo "<br>";
//output frequency list
foreach($ipalist as $ipa=>$fre)
echo "$ipa: $fre<br>";
echo "</td></tr></table>";
}
//Code to help search for "onset" "nuclei" and "coda"
//list onsets only
echo "Onsets only<br>";
for($col=0; $col<count($table[0]); $col++) {
$s = $table[0][$col];
if (whichSyllPart($s) == 'o') echo "$s ";
}
//list nuclei only
echo "Nuclei only<br>";
for($col=0; $col<count($table[0]); $col++) {
$s = $table[0][$col];
if (whichSyllPart($s) == 'n') echo "$s ";
}
//list codas only
echo "Codas only<br>";
for($col=0; $col<count($table[0]); $col++) {
$s = $table[0][$col];
if (whichSyllPart($s) == 'c') echo "$s ";
}
In order to restrict the search domain you need to enter the following code as part of the "//Find $IPAstr in $target" section of the code.
//Find $IPAstr in $target
for($i=1; $i<count($target); $i++)
{
if ($where == whichSyllPart($words[$i])){
if (mb_strpos($target[$i],$IPAstr) !== false)
$indices[] = $i;
}
else if ($where == "everywhere"){
if (mb_strpos($target[$i],$IPAstr) !== false)
$indices[] = $i;
}
}
For this to run you need a function whichSyllPart()
function whichSyllPart($sy)
{
$pt = $sy[strlen($sy)-1];
return($pt);
}
This adds an if/else if statement including the whichSyllPart() function that restricts the search according to the value of $where.

PHP How to delete an element from an array after copying in a new place?

I'm looping in an array, filling it with data from a file.
I'm trying to change the data from one cell to another and then leave the old cell empty.
So far I was using this code and it worked:
$custom_array_price['b'] = $custom_array_price['c'];
unset($custom_array_price['c']);
But for this specific case it doesn't work. It deletes the data before it gets copied.
How do I make it work?
This is the code I'm trying to use now:
$custom_array_price['c'] = $custom_array_price['d'];
unset($custom_array_price['d']);
The loop looks like this:
foreach ( $custom_array_price as $custom_key => $custom_value ) {
$custom_value = utf8_encode($custom_value);
if ( $custom_key == 'a' ){
if ( $custom_value == "Fecha:" ){
if( count($recibos) > 0 ){
$recibos[$cont]['html'].= "</tr>";
$recibos[$cont]['html'].= "</table>";
$cont++;
$times = 0;
}
$recibos[$cont]['html'] = "<table>";
$recibos[$cont]['html'].= "<tr>";
}else{
$recibos[$cont]['html'].= "<tr>";
}
}
if ( $times <= 5 ){
$custom_array_price['b'] = $custom_array_price['c'];
$custom_array_price['c'] = $custom_array_price['d'];
unset($custom_array_price['d']);
}
if ( $custom_value == "Neto a Pagar:"){
$custom_array_price['l'] = $custom_array_price['k'];
unset($custom_array_price['k']);
}
if ( $custom_value == "A001"){
$custom_array_price['g'] = $custom_array_price['i'];
unset($custom_array_price['i']);
}
$recibos[$cont]['html'].= "<td>";
$recibos[$cont]['html'].= $custom_value;
$recibos[$cont]['html'].= "</td>";
}
$recibos[$cont]['html'].= "</tr>";
Another part of the code. Where the csv file is being read and the data is being copied:
$recibos = array();
foreach($data_rows_price as $key => $value){
$times++;
for($i = 0; $i < count($value); $i++){
if(array_key_exists('mapping'.$i,$ret_array)){
if($ret_array['mapping'.$i]!='add_custom'.$i){
$new_post[$ret_array['mapping'.$i]] = $value[$i];
}
else{
$new_post[$ret_array['textbox'.$i]] = $value[$i];
$custom_array_price[$ret_array['textbox'.$i]] = $value[$i];
}
}
}
for($inc = 0; $inc < count($value); $inc++){
foreach($keys_price as $k => $v){
if(array_key_exists($v,$new_post)){
$custom_array_price[$v] = $new_post[$v];
}
}
}

need help with php loop logic

This code works perfectly except that it doesn't print the last 2 rows of my csv file:
This is the file:
603629,0,ATLV0008,"Vendor1",1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP
603627,2,ATLV0008,"Vendor1",1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP
600023,0,FLD4V0003,"Vendor2",1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP
600024,0,FLD4V0003,"Vendor2",1800.00,06/15/2010,1800.00,6/14/10 Request,EFT-JP
603631,0,ATLV5066,"Vendor2",1000.00,11/09/2010,1000.00,11/4/10 Check Request,PM2
603647,0,ATLV5027,"DVendor3",2799.80,11/15/2010,2799.80,10/1-10/31/10 Bishop,PM2
603642,5,ATLV5027,"Vendor3",482.40,11/15/2010,482.40,10/1-10/18/10 Allen,PM2
603653,0,ATLV0403,"Vendor4",931.21,11/17/2010,931.21,9/1-9/30/10,EFT-JP
603661,0,ATLV0105,"Vendor5",26.75,11/19/2010,26.75,093139,PM2
603660,1,ATLV0105,"Vendor5",5.35,11/19/2010,5.35,093472,PM2
Here is the code: (It needs to display the sum of 2 rows with the same vendor before the actual rows)
if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
while (($row_array = fgetcsv($handle, 1000, ","))) {
foreach ($row_array as $key => $val) {
$row_array[$key] = trim(str_replace('"', '', $val));
ob_flush();
}
$complete[] = $row_array;
ob_flush();
}
}
flush();
$prevVendor = '';
$sum = 0;
$venData = '';
if (isset($complete)) {
foreach ($complete as $key => $val) {
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != NULL){
print "<br/>";
print $sum . "<br/>";
print $venData;
$sum = 0; $venData = '';
}
}
foreach ($val as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . '&nbsp';
$prevVendor = $val[3];
}
}
if ($currVendor == $prevVendor){
$sum += $complete[$key][6];
}
}
}
I don't really understand your problem but if you want to get (and save it wherever you want) the sum of each vendors, you should do something like this :
$prevVendor = null;
$venData = '';
$sums = array();
if (isset($complete) && is_array($complete)) {
$lim = count($complete);
for ($key=0;$key<$lim;++$key) { // no need foreach for simple array
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != null){
print "<br/>";
print $venData;
}
$venData = '';
$prevVendor = $currVendor;
$sums[$currVendor] = 0; // set the counter to 0
}
foreach ($complete[$key] as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . ' '; // is useful for you I guess
}
}
$sums[$currVendor] += $complete[$key][6]; // add amounts
}
}
if ($prevVendor != null){ // you need to do this to display the last record
print "<br/>";
print $venData;
}
var_export($sums); // check results
This can be some syntax errors ...

Categories