不确定我是否可以这样做......
好吧我现在在这里运行循环来检查学生#和电子邮件我使用$ error_check = 0和$ error_check ++来考虑是否有错误。如果有,那么将显示一个错误,并且会将某些内容打印到error.log中,如果没有,那么类似.txt文件的内容就会打印出来并打印内容
但我刚刚意识到,如果我的.txt文件的第一行出错,或者如果我的.txt文件中的第二行内容出现错误,那么脚本将只能正常运行,然后打印正确的消息输出第一行内容,错误信息也将打印出来。
但当然我希望脚本能够运行,即使第一行内容没有错误而其他一些行有错误,那么只会打印错误信息。
我认为应该是关于我将脚本放在循环中的地方,但不知怎的,我继续阅读我的脚本,不知道我应该改变什么......
else //else file exist then.......else #1
{
$fileContents = file("./courses/" . $_GET["filename"]); //calls out the file into $fileContents[] array....so $fileContents[0] is like the first row of the contents
$datePrinted = false; //setting a flag first so the date will be print only once when there's error..being used later in the foreach loop
$error_message = false; //setting a flag so error message only show once and being used later in the foreach loop
$well_formed = false; //another flag to stop the looping message saying the file is correctly formed
$table = false;
$error_check = 0;
sort($fileContents);
foreach($fileContents as $row) //the contents will be then seen as $row
{
$column = preg_split("/,/", $row); //splits the row by , and name each array as $column
$error_log = fopen("./courses/path/error.log","a+") or die ("File can not be opened"); //creates/open an error.log file
if(!(isEmailAddressWellFormed($column[3])) || !(isStudentNumberWellFormed($column[0]))) //two functions calls from page4.php using preg_match to check email and student# validation
//if one of them do not validate the following will start
{
$error_check++;
if(!$error_message) //if this is false then the error message+link will show
{
echo "Errors have been found in " . $_GET['filename'] . "<br/><a href='./courses/path/error.log'>Click here for the log</a>";
$error_message = true; //since this is in a loop but doesn't need to print it out so many times so changing the value here to true so when this loop runs again if(!error_message) will not run again
}
if(!$datePrinted) //works as how error_message works. the date only print once by here
{
fwrite($error_log, date("F t, Y (h:i:s a)") . PHP_EOL);
$datePrinted = true;
}
if(!(isEmailAddressWellFormed($column[3]))) //checks again if the email is not valid then print the follow to error.log
{
fwrite($error_log, "Improper email address from" . $_GET["filename"] . " :" . PHP_EOL);
fwrite($error_log, "$column[2] $column[1] $column[3]" . PHP_EOL);
}
if(!(isStudentNumberWellFormed($column[0]))) //checks again if the student # is not valid then print the follow to error.log
{
fwrite($error_log, "Improper student numbers from" . $_GET["filename"] . " :" . PHP_EOL);
fwrite($error_log, "$column[2] $column[1] $column[0]" . PHP_EOL . "\n");
}
}
elseif($error_check == 0)
{
if(!$well_formed)
{
echo "<h1 style='color: red'>" . $_GET["filename"] . " is well formed.</h1>";
echo "<table>";
$well_formed = true;
}
echo $column[0];
echo $column[1];
echo $column[2];
echo $column[3];
}
}//closing foreach
fwrite($error_log, str_repeat("-",80) . PHP_EOL); //puts a line to separate the next error but each time the page runs this line will print even if there's no error
fclose($error_log);
if(!$table)
{
echo "</table>";
$table = true;
}
}//closing else #1
事先感谢人们的帮助......谢谢....
我添加了
$row = $column[0] . $column[1] . $column[2] . $column[3];
在preg_split之后的foreach开头,然后在底部,我改为...
elseif($error_check == 0)
{
$row.=$row;
}
}//closing foreach
fwrite($error_log, str_repeat("-",80) . PHP_EOL); //puts a line to separate the next error but each time the page runs this line will print even if there's no error
fclose($error_log);
if($error_check == 0)
{
echo "<h1 style='color: red'>" . $_GET["filename"] . " is well formed.</h1>";
echo "<table>";
echo $row;
echo "</table>";
}
}//closing else #1
?>
但现在它只打印出内容O.o中的第二行。目前只有三行内容供测试....我在这里遗漏了一些小事吗?