Avatar of iceman19330
iceman19330
 asked on

php writing to excel file row limit

I was wondering if there is a way to break up a csv file so that when it gets to 65000 rows it will start to create a 2nd csv file.  the editing needs to be done in excel and the less I have to ask them to manually do on their own the less hassel I get.
$out = '';
$fields = mysql_list_fields('','',$db);
$columns = mysql_num_fields($fields);
 
// Put the name of all fields
for ($i = 0; $i < $columns; $i++) {
$l=mysql_field_name($fields, $i);
$out .= '"'.$l.'",';
}
$out .="\n";
 
// Add all values in the table
while ($l = mysql_fetch_array($rsSearchResults)) {
for ($i = 0; $i < $columns; $i++) {
$out .='"'.$l["$i"].'",';
}
$out .="\n";
}
// Output to browser with appropriate mime type, you choose ;)
//header("Content-type: text/x-csv");
header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=search_results.csv");
echo $out;
exit;

Open in new window

PHP

Avatar of undefined
Last Comment
Sandeep Kothari

8/22/2022 - Mon
Sandeep Kothari

try modifying the php code as follows.. might work...
// Put the name of all fields
for ($i = 0; $i < $columns; $i++) {
$l=mysql_field_name($fields, $i);
$hlist .= '"'.$l.'",';
}
$hlist .="\n";
 

// Add all values in the table
$m = 1;
$out = $hlist;
while ($l = mysql_fetch_array($rsSearchResults)) {
  for ($i = 0; $i < $columns; $i++) {
    $out .='"'.$l["$i"].'",';
  }
  $out .="\n";
  $m++;
  if($l > 65000){
     header("Content-type: text/csv");	
     header("Content-Disposition: attachment; filename=search_results.csv");
     echo $out;
     $out = $hlist;
  }
}

Open in new window

exit;
Sandeep Kothari

missed resetting $m ...

also reset $m in the condition of 65000 ...
try this.
try modifying the php code as follows:
// Put the name of all fields
for ($i = 0; $i < $columns; $i++) {
$l=mysql_field_name($fields, $i);
$hlist .= '"'.$l.'",';
}
$hlist .="\n";
 

// Add all values in the table
$m = 1;
$out = $hlist;
while ($l = mysql_fetch_array($rsSearchResults)) {
  for ($i = 0; $i < $columns; $i++) {
    $out .='"'.$l["$i"].'",';
  }
  $out .="\n";
  $m++;
  if($l > 65000){
     header("Content-type: text/csv");	
     header("Content-Disposition: attachment; filename=search_results.csv");
     echo $out;
     $m = 1;
     $out = $hlist;
  }
}
exit;

Open in new window

iceman19330

ASKER
the 2nd file was looping out of control so not sure what happened there.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Sandeep Kothari

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question