validating the contents of an array

Discussion in 'PHP' started by derek34, Nov 14, 2007.

  1. #1
    Hi I need to figure out a way to validate an array and then display a message depending on the outcome of the validation. So far I've got:
    also, it needs to be done in two seperate files.

    
    //part of page3.php
    $readfile = file($file);
    	
    		foreach ($readfile as $string)
    		{
    		list($num, $last, $first, $email) = explode(',', $string);
    		require_once("./page4.php");
    		isStudentNumberWellFormed($num);
    		}
    
    PHP:
    <?php
    //page4.php
    function isStudentNumberWellFormed ($studentnumber){
    
    if(eregi("^a[0-9]{8}$", $studentnumber)){
    
    $openfile = fopen("courses/errors.log", "a+");
    $date_time = date("F d, Y (h:i:s a)\r\n");
    $filename = $_GET['filename'];
    $error = $date_time."Improper student number from:".$filename."\r\n".$studentnumber."\r\n------\r\n";
    
    fwrite($openfile, $error);
    fclose($openfile);
    echo "Errors have been found in".$_GET['filename'].".A report, errors.log has been generated";
    }
    
    else{
    $numberokay = "Error in string format";
    return $numberokay; 
    }
    
    }
    
    ?>
    PHP:
    As you can see on page4.php, if I put echo after I write to the error log, the foreach loop on page3.php will echo "Errors have been..." as many times as it loops through.
    I only need to display it once.
    Help!!:confused:
     
    derek34, Nov 14, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Please don't post multiple threads for the same problem. Bump your old ones.

    Plus the logic of your script doesn't make too much sense.

    Your function called isStudentNumberWellFormed should return either true or false. Don't output any errors there.

    And then inside your loop use an if() clause.

    
    
    if (!isStudentNumberWellFormed($num))
    {
        echo 'Error message here';
        break; // This will exit the loop
    }
    
    PHP:
    And call require_once before the loop. There's no need to include it each time new.
     
    nico_swd, Nov 14, 2007 IP
  3. derek34

    derek34 Guest

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ok thanks nico
    my apologies for the multiple thread postings, didn't know you could bump them up like that...
     
    derek34, Nov 14, 2007 IP