douxian1892 2012-10-21 18:52
浏览 63
已采纳

PHP搜索学生ID和姓氏

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>
";
?>
  • 写回答

2条回答 默认 最新

  • duanboniao5903 2012-10-21 19:16
    关注

    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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输