I have an assignment that has a form that takes ID and Last name as inputs. I want to take both the ID and Last Name and match it with the text file. If it matches, show that student in a table. If it doesn't match, echo that the student isn't found. So far, I got almost everything done, but everytime I search a student that exists, the student that isn't found is echoed.
Here is the link to my form: http://hills.ccsf.edu/~ryan/a7p1.php
Here is the class of students that you can get the ID and Last name from. The first column is the Student ID and the second column is the Last Name: http://fog.ccsf.edu/~tboegel/showclass.php
Here is the code:
<?php
$lname = $_REQUEST['lname'];
$id = $_REQUEST['id'];
function DisplayRow($ID) {
print "<tr>
";
$parts = split(":", $ID);
for($i=0; $i <=7; $i++) {
print "<td>$parts[$i]</td>
";
}
print "</tr>
";
}
$handle = fopen("class.txt", "r");
$line = fgets($handle);
while(!feof($handle)) {
$part = explode(":", $line);
if($id == $part[0] && $lname == $part[1]) {
echo "<table border='1' width='95%'>
<tr>
<th>Student ID</th>
<th>Student Last Name</th>
<th>Student First Name</th>
<th>Midterm 1</th>
<th>Midterm 2</th>
<th>Midterm 3</th>
<th>Final Exam</th>
<th>Letter Grade</th>
</tr>";
DisplayRow($line);
} else {
print "The person you are trying to search for does not exist";
die;
}
$line = fgets($handle);
}
fclose($handle);
print "</table>
";
?>
the while seaches till it finds something then it breaks... if the eof is reached it will trigger an error
<?php
$lname = $_REQUEST['lname'];
$id = $_REQUEST['id'];
function DisplayRow($ID) {
print "<tr>
";
$parts = split(":", $ID);
for($i=0; $i <=7; $i++) {
print "<td>$parts[$i]</td>
";
}
print "</tr>
";
}
$found = false;
$handle = fopen("class.txt", "r");
$line = fgets($handle);
while(!feof($handle)) {
$part = explode(":", $line);
if($id == $part[0] AND $lname == $part[1]) {
echo "<table border='1' width='95%'>
<tr>
<th>Student ID</th>
<th>Student Last Name</th>
<th>Student First Name</th>
<th>Midterm 1</th>
<th>Midterm 2</th>
<th>Midterm 3</th>
<th>Final Exam</th>
<th>Letter Grade</th>
</tr>";
DisplayRow($line);
echo "</table>";
$found = true;
break; //no need to go further already found the data
}
$line = fgets($handle);
}
if($found == false) {
echo "The person you are trying to search for does not exist";
}
fclose($handle);
?>
Your issue is simple ..... you are terminating the loop prematurely
else {
print "The person you are trying to search for does not exist"; //??
die;
} ^--------------- This your issue (Remove)
Better Still Use a database like SQLite
or MySQL
Please also note that split
is depreciated ,i'll advice to use explode
instead
$parts = split(":", $ID);
^------------- Change to explode