0

我有一个作业,其表格以 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";
?>
4

2 回答 2

1

您的问题很简单.....您过早地终止了循环

else {
        print "The person you are trying to search for does not exist"; //??
        die;
    }    ^--------------- This your issue (Remove)

最好还是使用数据库,例如SQLiteorMySQL

另请注意split已折旧,我建议explode改用

$parts = split(":", $ID);  
           ^-------------  Change to explode 
于 2012-10-21T19:07:30.363 回答
1

while 搜索直到它找到一些东西然后它会中断......如果达到 eof 它将触发一个错误

<?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);

?>
于 2012-10-21T19:16:33.270 回答