PHP : If...Else...Query

658

I am executing this statement underwhile (($data=fgetcsv($this->fin,5000,";"))!==FALSE)

Now what I want in else loop is to throw exception only for data value which did not satisfy the if condition. Right now am displaying the complete row as I am not sure how to throw exception only for data which does not satisfy the value.

Code

if ((strtotime($data[11]) &&strtotime($data[12])&&strtotime($data[16]))!==FALSE 
&& ctype_digit($data[0]) && ctype_alnum($data[1]) && ctype_digit($data[2]) 
&& ctype_alnum($data[3]) && ctype_alnum($data[4]) && ctype_alnum($data[5]) 
&& ctype_alnum($data[6]) && ctype_alnum($data[7]) && ctype_alnum($data[8]) 
&& $this->_is_valid($data[9]) && ctype_digit($data[10]) && ctype_digit($data[13]) 
&& $this->_is_valid($data[14]))
{
     //Some Logic
}
else
{
    throw new Exception ("Data {$data[0], $data[1], $data[2], $data[3],
    $data[4], $data[5], $data[6], $data[7],
    $data[8], $data[9], $data[10], $data[11], $data[12],
    $data[13], $data[14], $data[16]} is not in valid format");
}

Guidance would be highly appreciated as to how can I throw exception only for data which did not satisfy the if value.

902

Answer

Solution:

Why not separate the tests ? i.e. do each test one by one, and throw an exception if a specific test fails ?


Copy-pasting from your code, it would probably look like this :

if (!strtotime($data[11])) {
    throw new Exception("field 11 : {$data[11]} is not a valid date");
}
if (!strtotime($data[12])) {
    throw new Exception("field 12 : {$data[12]} is not a valid date");
}
// Some more...
if (!ctype_alnum($data[8])) {
    throw new Exception("field 8 : {$data[8]} is not a valid alnum");
}
// And so on...

// And when all is tested, you know the items 
// in $data are all OK


This way :

  • You can know which field caused a validation failure
  • If you have several distinct tests on the same field, you can know which specific test failed

And, as a possibility, you could (if needed) thrown different kind of exceptions, depending on the test that failed (i.e. one kind of exception for dates, one for integers, ...) -- in some cases, that might be useful.



Edit after the comment : more full example

Yes, you can validate field by field, and still work line by line.

You just have to wrap your testing code in a try/catch block, that's inside the loop that goes line by line ; a bit like that :

$validData = array();
$errors = array();

while ($data = fgetcsv($f)) {
    try {
        // Validate the data of the current line
        // And, if valid, insert it into the database

        if (!strtotime($data[11])) {
            throw new Exception("field 11 : {$data[11]} is not a valid date");
        }
        if (!strtotime($data[12])) {
            throw new Exception("field 12 : {$data[12]} is not a valid date");
        }
        // Some more...
        if (!ctype_alnum($data[8])) {
            throw new Exception("field 8 : {$data[8]} is not a valid alnum");
        }
        // And so on...

        // And when all is tested, you know the items 
        // in $data are all OK
        // => which means it can be inserted into the DB
        // Or you can just put the valid data into a "temporary" array, that will be used later :
        $validData[] = $data;

    } catch (Exception $e) {
        // An error has occurend on the current line
        // You can log it, if necessary :
        $errors[] = $e->getMessage();
    }
}

// Now that the whole file has been read, test if there's been an error :
if (empty($errors)) {
    // No error
    // => insert the data that's in $validData
} else {
    // There's been an error
    // => don't insert the data that's in $validData, if you don't want to insert anything
}


With that :

  • If there is an exception on one line (i.e. validation fails), you'll jump to the catch block, to deal with the problem
  • And the loop will then restart, for the next line.

EDIT: (By Yacoby)
Rather than having endless if statments, you could just define which elements should be checked by which function and then process them in a loop. That way it avoids having 16 if statements.

Code example:

foreach ( array(11,12,16) as $index ){
    if ( !strtotime($data[$i]) ){
        throw new Exception("field {$i} : {$data[$i]} is not a date");
    }
}

foreach ( array(1,3,4,5,6,7,8) as $index ){
    if ( !ctype_alnum($data[$i]) ){
        throw new Exception("field {$i} : {$data[$i]} is not alphanumeric");
    }
}

foreach ( array(2, 10) as $i ){
    if ( !ctype_digit($data[$i]) ){
        throw new Exception("field {$i} : {$data[$i]} is not a valid digit");
    }
}

if ( !$this->_is_valid($data[14]) ){
    throw new Exception("field {14} : {$data[14]} is not valid");
}
909

Answer

Solution:

You'll need to break up your massiveif statement into one per value.

947

Answer

Solution:

I think you'd be better off breaking this up into many if statements like

if(!strtotime($data[11])
{
    throw new Exception("...");
}

if(!strtotime($data[12]))
{
    throw new Exception("...");
}


//after all of your if statements now do business logic
//remember that all of your if conditions have to be met to get this far
//because throwing the exception will leave this stack
//so it functions sort of like the else clause would.

People are also looking for solutions to the problem: php - dropbox API upload NO oAuth possible?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.