Hi I am currently taking a PHP class. There is one assignment in particular that is giving me trouble. Instead of trying to explain it all, I will list the part of the assignment I am having trouble with(it's kind of long and complicated but any help is appreciated) So here's what I've got so far: <?php //page3.php if ($_GET['filename']) { $file = "./courses/".$_GET['filename']; if (file_exists($file)){ $readfile = file($file); foreach ($readfile as $oneline) { list($num, $last, $first, $email) = explode(',', $oneline); require_once("./page4.php"); isStudentNumberWellFormed($num); } }else { echo "no such file, sorry"; } } else { die('must specify filename'); } ?> PHP: and here's page 4: <?php function isStudentNumberWellFormed ($studentnumber){ if (eregi("a",$num)) { continue; } else{ $openfile = fopen("courses/errors.log", "a+"); fwrite($openfile, $studentnumber); fclose($openfile); die("errors have been found in ".$_GET['filename']); } } ?> PHP: But what I can't figure out is how to loop through each $num array without displaying the message to the user a bunch of times. Also, how can I write the date and time on a new line in the text file? Thanks in advance for your time.
Derek34, I tried running your code but did not encounter the problem that you describe (multiple messages from looping through $num). However, I do have a few comments for you: 1) In page4.php your eregi() is looking at $num but $num is not available to you while you are inside this function. When you are inside a function you can not access variables from outside or other functions unless you pass them in or make them global. You did pass $num to isStudentNumberWellFormed(), but as $studentnumber, so you should change your eregi() to look at $studentnumber, not $num. You might also run into a problem when you print your error message because you are using $_GET, which isn't global or passed into isStudentNumberWellFormed(). 2) Right now your eregi() pattern might not be doing what you expect it to. Regular expressions can be hard to grasp, especially when you are learning something else like php at the same time. From your professor's notes it sounds like he wants student numbers to match the letter A followed by 8 numbers.. like "A00238123". A regex that might do this would be something like '^a[0-9]{8}$'. The carrot means "starts with", the [0-9] means anything in the range of 0-9, the {8} means repeated 8 times and the dollar sign means the end. Check out this regex tutorial for more info. 3) As for your question about the date and new lines, you can get a readable timestamp from date("m/d/Y h:i:s: ") PHP: and putting "\n" in a string will result in a carriage return to start a new line. So maybe something like the following to write to your log: fwrite($openfile, date("m/d/Y h:i:s") . " " . $studentnumber . "\n"); PHP: 4) I also noticed that you are doing your require_once("./page4.php") from inside a loop.. while this does do the job, it isn't really good programming.. since you only need to do the require one time, put it outside of any loops, the first line after <?php is probably a good spot for it. Hope this helps!
Hey thanks Drunnells! That info helped quite a bit. You're right about the regex, it's somewhat complicated to do especially if you're learning a whole programming language like that. As for the problem I encountered with displaying the error message a number of times, I used the die() function which means that after validating the first array, it ends the script. That would not accomplish what I am trying to do, however if I just echo the error message out, it displays it a number of times(depending on how many errors it found) But just to let you know, I think I have figured out a way to make it work. if I add a counter and get the error message to display when the counter reaches 1, then it will display the message only once regardless of how many errors were found. But again, thanks for your help and I hope I can contact you in the future for php help.
Any time man! Hey, if you found this useful feel free to leave me some positive iTrader feedback.. (i'm new here)
Hey, my counter idea didn't work. it kept resetting the counter to 0 every time it went through the loop: <?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); $counter = 0; $counter++; if($counter == 1){ echo "Errors have been found in ".$_GET['filename'].".A report, errors.log has been generated"; } } else{ echo "All data is fine"; } } ?> PHP: help?
I think that you should probably move the logic that decides to print an error message to outside the isStudentNumberWellFormed() function and then have isStudentNumberWellFormed() return a 1 or 0 depending on if it encountered an error or not. At the end of the loop you can then decide to print an error based on how many errors isStudentNumberWellFormed() says it found. Like so: page3.php: <?php //page3.php if ($_GET['filename']) { $file = "./courses/".$_GET['filename']; if (file_exists($file)){ $readfile = file($file); $error_counter = 0; foreach ($readfile as $oneline) { list($num, $last, $first, $email) = explode(',', $oneline); require_once("./page4.php"); $error_counter = $error_counter + isStudentNumberWellFormed($num); } if ($error_counter > 0) { echo "Errors have been found in ".$_GET['filename'].".A report, errors.log has been generated"; } else { echo "All data is fine."; } }else { echo "no such file, sorry"; } } else { die('must specify filename'); } ?> PHP: page4.php: <?php function isStudentNumberWellFormed ($studentnumber){ $errors = 0; 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); $errors = 1; } return $errors; } ?> PHP: So to explain what I have changed, in page4.php i removed the error message logic and your counter and replaced it with an $errors variable which is initially set to 0 at the top but then set to 1 if an error is encountered. At the end of the function I return the value of $errors. Then in page3.php I set $error_counter = 0 before the validation loop. $error_counter has the $errors result from isStudentNumberWellFormed() added to it each time the loop is run, if there are no errors $error_counter remains 0. I moved your error message logic from page4.php to right after this loop; if $error_messages is greater than 0 you get the error message, if not you get the "all data is fine" message. Oh, in page4.php i changed your if(), you had the code for when errors are encountered inside of: if(eregi('^a[0-9]{8}$', $studentnumber)){ } PHP: But eregi() here will be true when it passes the test, not when it fails.. so i added a "!" to the beginning of the conditional to reverse the check: if(! eregi('^a[0-9]{8}$', $studentnumber)){ } PHP: Or we could have just as easily moved everything from before the "else" to after and vis versa.