Search code examples
phpfopenfwritefclose

writing error log but I'm not sure how to organize it


I have two functions doing preg_match to check if the emp# and the email is valid. If one or both is not valid it will be printed to an error.log but I want to organize it in a way humm....let me see if I know how to example as simple as possible.

Let's say if the emp# is not valid then the error log will show

date()
emp#

if the email is not valid then the error log will show

date()
email

if both are not valid then the error log will show

date()
emp#
email

The thing is when both happens I don't want it to print the date twice or more than one emp# or email is not valid the date will not repeat will only print like

date()
emp#
emp#
emp$

hopefully my explanation make sense...what I have now is

    if(((isEmailAddressWellFormed($column[3]) == false)) && ((isStudentNumberWellFormed($column[0]) == false)))
    {
        $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
        fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
        fwrite($filehandle, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
        fwrite($filehandle, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[0]\n" . PHP_EOL);
        fclose($filehandle);        
    }
    else
{
    if(isEmailAddressWellFormed($column[3]) == false)
    {
        $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
        fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
        fwrite($filehandle, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
        fclose($filehandle);
    }

    if(isStudentNumberWellFormed($column[0]) == false)
    {
        $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
        fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
        fwrite($filehandle, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[0]\n" . PHP_EOL);
        fclose($filehandle);
    }
}

but I know it's not getting what I want....any simple way I can make it happen?


Solution

  • $errors = array();
    
    if(!isEmailAddressWellFormed($column[3]))
    {
        $errors[] = "Improper email address from " . $_GET["filename"] . " :";
        $errors[] = "$column[2] $column[1] $column[3]";
    }
    
    if(!isStudentNumberWellFormed($column[0]))
    {
        $errors[] = "Improper student numbers from " . $_GET["filename"] . " :";
        $errors[] = "$column[2] $column[1] $column[0]";
    }
    
    if (!empty($errors))
    {
        $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
        fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
        fwrite($filehandle, implode(PHP_EOL, $errors);
        fclose($filehandle);
    }