0

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?

Dora
  • 6,776
  • 14
  • 51
  • 99

3 Answers3

1
$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);
}
Maks3w
  • 6,014
  • 6
  • 37
  • 42
  • I tried the code but somehow it's still giving me two dates if two errors in the same field...:( – Dora Feb 24 '13 at 08:24
  • Then you are calling twice the same fragment of code because is impossible print the date twice with the above snippet – Maks3w Feb 24 '13 at 09:21
  • If you are in a loop or something just put the `if (!empty($errors))` outside of the loop block – Maks3w Feb 24 '13 at 09:23
  • umm....weird then >.<" I just copy and pasted your code into my script to try.... – Dora Feb 24 '13 at 09:23
  • $errors declaration must go before the loop (`$errors = array();`) if not the value is reset in each iteration. – Maks3w Feb 24 '13 at 09:27
  • $error[] append the value to the end of the array. Let me suggest you to review your concepts about loops and arrays. – Maks3w Feb 24 '13 at 14:03
  • yup will have to do that soon esp. array when it starts to get complicated it confuses me... – Dora Feb 24 '13 at 21:15
0

There are many ways you could deal with this. One way you could do it is like this:

$error = false;

if (!isEmailAddressWellFormed($column[3]))
{
    $emailError = "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL . "$column[2] $column[1] $column[3]\n" . PHP_EOL;
    $error = true;
}

if (!isStudentNumberWellFormed($column[0]))
{
    $studentError = "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL . "$column[2] $column[1] $column[0]\n" . PHP_EOL;
    $error = true;
}

if ($error)
{
    $handle = 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);

    if (isset($emailError))
        fwrite($handle, $emailError);

    if (isset($studentError))
        fwrite($handle, $studentError);

    fclose($handle);
}
Supericy
  • 5,866
  • 1
  • 21
  • 25
  • I tried the code but somehow it's still giving me two dates if two errors in the same field...:( – Dora Feb 24 '13 at 08:23
0

try to use a log class, to write errors

https://github.com/soroushatarod/Loggerr

meWantToLearn
  • 1,691
  • 2
  • 25
  • 45