我有一个分配,其表单将ID和姓氏作为输入。我想同时使用ID和姓氏,并将其与文本文件匹配。如果匹配,则在表格中显示该学生。如果不匹配,则回显未找到学生。到目前为止,我几乎完成了所有工作,但每当我搜索一个存在的学生时,找不到的学生就会得到回应。
以下是我的表单的链接:http://hills.ccsf.edu/~ryan/a7p1.php
以下是您可以从中获取ID和姓氏的学生。第一列是学生ID,第二列是姓氏:http://fog.ccsf.edu/~tboegel/showclass.php
以下是代码:
<?php
$lname = $_REQUEST['lname'];
$id = $_REQUEST['id'];
function DisplayRow($ID) {
print "<tr>\n";
$parts = split(":", $ID);
for($i=0; $i <=7; $i++) {
print "<td>$parts[$i]</td>\n";
}
print "</tr>\n";
}
$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>\n";
?>
答案 0 :(得分:1)
你的问题很简单.....你过早地终止了循环
else {
print "The person you are trying to search for does not exist"; //??
die;
} ^--------------- This your issue (Remove)
更好使用SQLite
或MySQL
等数据库
另请注意,split
已弃用,我建议您使用explode
$parts = split(":", $ID);
^------------- Change to explode
答案 1 :(得分:1)
<?php
$lname = $_REQUEST['lname'];
$id = $_REQUEST['id'];
function DisplayRow($ID) {
print "<tr>\n";
$parts = split(":", $ID);
for($i=0; $i <=7; $i++) {
print "<td>$parts[$i]</td>\n";
}
print "</tr>\n";
}
$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);
?>