Te lo he reorganizado, y limpiado, con una mejor gestión de los casos especiales.
Código PHP:
Ver original<html>
<head>
<title>Problema</title>
</head>
<body>
<?php
$cabecera="cabecera\n\n";
$pie="Este es mi pie";
$counter=0;
// Si no existe ningun fichero, estamos al principio.
{
$index=-1;
$currentFile=null;
}
else
{
// Existe al menos 1 fichero.
$k=0;
{
$k++; // Este es el bucle.
}
// Al salir del bucle, k indica que fichero NO existe.Asi que, el ultimo que existe es k-1.
$k--;
// Se abre el fichero, y se cuenta el numero de líneas que contiene.
$lines=file("./miFichero".$k.".txt"); foreach($lines as $value)
$counter+=($value[0]=='#'?1:0);
$index=$k;
$currentFile=fopen("miFichero".$index.".txt","a"); }
// $res es el resultado de mysql_query de tu consulta.
for($k=0;$k<23;$k++)
{
if($counter%NLINES==0)
{
if($currentFile!=null)
{
}
$index++;
$currentFile=fopen("miFichero".$index.".txt","w");
fwrite($currentFile, $cabecera); }
fwrite($currentFile, "# ".$k." -- ".$counter."\n\n"); $counter++;
}
if($currentFile)
{
if($counter%NLINES==0)
}
?>
</body>
</html>