我被困住了。你会看到我有 var *$file_count_header*。我只是想让它的数量随着与 *$file_count* 相同的计数增加(理想情况下只使用相同的变量)。*$file_count* 在 if() 中按预期增加但是正如您所见,我的标题内容不在循环中。我通过将整个代码放在另一个循环中尝试了几个解决方案,但我无法让它继续工作。
$results_count = mysql_num_rows($result);
$i = 0;
$file_count = 1;
$per_file = 100;
$footer = 'FOOTER';
$default_contents = $contents = array("HEADER . $file_count_header");
while ($row = mysql_fetch_array($result)) {
// Build the line contents here
$line = $row['id'];
$contents[] = $line; // Each array element will be a line in the text file
$i++;
// Every $per_file number of records, write the contents to the file and start counting over
if ($i == $per_file) {
$contents[] = $footer; // Add the footer to the end
file_put_contents($results_count .'-'. $file_count .'.txt', implode("\r\n", $contents));
// Reset the counter and set the contents to the
$i = 0;
$contents = $default_contents;
$file_count++;
$file_count_header++;
}
}
// Output the rest of the contents to the last file
$contents[] = $footer; // Add the footer to the end
file_put_contents($results_count .'-'. $file_count .'.txt', implode("\r\n", $contents));