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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办