not sure if I can example this right...
alright what I have here now runs the loop to check the student# and email I used an $error_check = 0 and $error_check ++ to consider if there's an error. If there is then an error will show and something will be printed into an error.log if not then something like the .txt file is good will print out and prints the contents
But I just realized the script will only run properly IF there's an error in the first row of my .txt file or else if there's an error to my second+ rows of contents in my .txt file then for the correct message will print out with the first row of content AND the error message will print out too.
But of course I want it the script to run that even if the first row of contents has no error and some other rows have error then only the error message will be printed.
I think it should be something about where I put the scripts in the loop but somehow I keep on reading my script and no idea what I should change....
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 . "
");
}
}
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
thanks in advance for people helping...thanks....
I added
$row = $column[0] . $column[1] . $column[2] . $column[3];
at the beginning of the foreach after the preg_split then at the bottom I changed to...
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
?>
but now it only prints out the second row in the contents O.o only have three rows of contents at the moment for testing....am I missing something minor here?