我被困住了。你会看到我有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));
答案 0 :(得分:0)
初始化
$file_count_header = 1;
位于顶部,$file_count
喜欢
$file_count = 1;
$file_count_header = 1
您遇到了问题,因为您尚未初始化$file_count_header
,即使它仅在if
内的while
中使用。所以它应该在while
之外用默认值声明。