Need some good troubleshooting eyes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pendragon
Forum Newbie
Posts: 18
Joined: Mon Mar 19, 2007 3:25 pm

Need some good troubleshooting eyes

Post by pendragon »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Question:  This script properly gives error messages if form fields are left blank or filled with non-matching information (student ID's and last names) to the data file being read.  I am apparently having problems with my braces or my if else statement placement, because I cannot get  a print out of data from the data file with correct matches of ID and last name (both must match).  Can anyone spot my errors?  I've been juggling this script around for quite some time but am not making headway.  How can I simply and correct my script?

Thanks.

Code: Select all

<?php	
/*
*  This program consists of a form that allows the user to enter their
 *  last name and student ID number.
 *
 *  The script processing the form displays the student's grades in a data
 *  file that matches both the last name and the ID number entered.  
 *
 *  Error Messages:  If no matches are found on both the last name and student
 *  ID, an appropriate error message is displayed.
 *  
 */
?>
<html>
<head>
<title>Find Your class Information</title>
</head>
<body >
<h2>Your Class Information</h2>
<table border="1">
<tr>
<th rowspan=2>Student<br />ID</th>
<th rowspan=2>Last<br />Name</th>
<th rowspan=2>First<br />Name</th>
<th colspan=3>Midterm</th>
<th rowspan=2>Final<br />Exam</th>
<th rowspan=2>Letter<br />Grade</th>
</tr><tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<?php
    //  First, Retrieve Form Variables
	       $lastName  = trim($_POST['name']);
	       $studentID = trim($_POST['id']); 
   	
    // Then open the file - for reading
	   $readfile=fopen("class.dat", "r") or die("Sorry, the file could not be opened.");
	    while(!feof($readfile)){ 
		       $line = fgets($readfile); 
			    
		     //  break data lines apart
		         $fields=explode(":", $line);
			 //  name field items
				 $id=$fields[0];
				 $surName=$fields[1];
				 $firstName=$fields[2];
				 $midterm1=$fields[3];
				 $midterm2=$fields[4];
				 $midterm3=$fields[5];
				 $finalgrade=$fields[6];
				 $lettergrade=$fields[7];
		}  //  End of while statement	
			
			 //  check for match with student last name
			     if( preg_match("/^$lastName/","$surName")){
							   		
			    //  also check for match with student ID
				    if ( preg_match("/^$studentID/","$id")){
					}  //	 End of internal if statement
				    else { print "<p><h3>No information will be printed.<br />
				           Your name and ID combination does not match our data.</h3></p>";}				                 }  //  End of 1st if statement
				 else { print "<p><h3>Your name and ID combination does not match our data.                                  Try Again!</h3></p>";}
			     
				   
			//  print while statements if both are true
			    //  print table rows
			            print "<tr>";
				        print "<td>$id</td>";
						print "<td>$surName</td>";
						print "<td>$firstName</td>";
						print "<td>$midterm1</td>";
						print "<td>$midterm2</td>";
						print "<td>$midterm3</td>";
						print "<td>$finalgrade</td>";
						print "<td>$lettergrade</td>";
					    print "</tr>\n";	
					     
            fclose($readfile);
		   
?></table>    
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

after
$readfile=fopen("class.dat", "r") or die("Sorry, the file could not be opened.");
while(!feof($readfile)) {
$line = fgets($readfile);

// break data lines apart
$fields=explode(":", $line);
// name field items
$id=$fields[0];
$surName=$fields[1];
$firstName=$fields[2];
$midterm1=$fields[3];
$midterm2=$fields[4];
$midterm3=$fields[5];
$finalgrade=$fields[6];
$lettergrade=$fields[7];
} // End of while statement
$id, $surName ... $lettergrade always have the values of the last record ...only the last record.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm pretty tired of editing your posts pendragon. Please shape up, quickly.
Post Reply