搜索数组中的表单输入

时间:2015-09-13 03:24:01

标签: php arrays forms

编写一个包含三个并行数组的应用程序,每个数组包含10个元素。第一个数组包含四位数的学生ID号,第二个数字包含名字,第三个数字包含学生的平均成绩。接受学生证号码并显示学生的名字和成绩点平均值。如果未找到匹配项,请显示包含错误ID号的相应错误消息,并允许用户搜索新的ID号。

这是我未完成的代码:

<html>
<head>
</head>
<body>
<?php
$student = array (
$a = array ("F001"=>"a","F002"=>"b","F003"=>"c","F004"=>"d","F005"=>"e","F006"=>"f","F007"=>"g","F008"=>"h","F009"=>"i","F010"=>"j"),
array ("albert","berto","charlie","david","earl","francis","garry","harry","irish","james"),
array (1,2,3,3,2,1,2,1,3,1)
);

if (isset($_POST['search'])){
$idnumber = $_POST['search'];
if ($idnumber == $a){
    echo array_search("a",$student,true);}
else {
    echo "id number not found";}}

echo ('<form action="" method="POST">');
echo ('Id number: <input type="text" name="search">');
echo ('<input type="submit" value="search">');
echo ('</form>');
?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

尝试为学生阵列设置更好的结构。

<?php

$students = [
    ['F001', 'albert', 1],
    ['F002', 'berto', 2],
    ['F003', 'charlie', 3]
];

if(isset($_POST['search'])) {
    $id = $_POST['search'];
    $student = array_search($id, array_column($students, 0));

    if($student) {
        echo "student number $id with name " . $students[$student][1] .
             " has gpa " . $students[$student][2];
    }

    else {
        echo "student $id not found";
    }
}


echo ('<form action="" method="POST">');
echo ('Id number: <input type="text" name="search">');
echo ('<input type="submit" value="search">');
echo ('</form>');

?>

当您需要有关函数参数和返回值的信息时,请参阅php.net文档。

答案 1 :(得分:0)

*如果您选择接受它,您的任务是编写一个包含三个并行数组的应用程序,每个数组包含10个元素。

  • 第一个数组包含四个字符的学生ID,第二个数字包含名字,第三个数字包含学生平均成绩点数。
  • student ids数组中的第一个元素对应于name数组中的第一个元素,对应于grade数组中的第一个元素,依此类推。
  • 如果找到,应用程序应接受学生ID并显示学生的名字和成绩点平均值。
  • 如果未找到匹配项,请显示包含无效ID号的相应错误消息,并允许用户搜索新的ID号。

    <!DOCTYPE html>
    <html>
        <head>
        </head>
        <body>
        <?php
            if (isset($_POST['search'])) {
                // move $students into the if statement cause we won't 
                // need it unless they're searching
                $students = array (
                    array ("F001"=>"a","F002"=>"b","F003"=>"c","F004"=>"d","F005"=>"e","F006"=>"f","F007"=>"g","F008"=>"h","F009"=>"i","F010"=>"j"),
                    array ("albert","berto","charlie","david","earl","francis","garry","harry","irish","james"),
                    array (1,2,3,3,2,1,2,1,3,1)
                );
    
                $idNumber = $_POST['search'];
                // we can use isset here because the student id *is* the key.
                // if it was the value, than we would use array_search() and
                // check if it returned false
                if (isset($students[0][$idNumber])) {
                    // array_keys returns the keys of an array as an array,
                    // allowing us to find the numerical index of the key
                    $studentIndex = array_search($idNumber,array_keys($students[0]));
                    // printf basically allows for formatted echoing. %s means
                    // a string. %d means a number. You then pass in your
                    printf('Student ID: %s<br>Name: %s<br>Grade: %d', $idNumber, $students[1][$studentIndex], $students[2][$studentIndex]);
                }
                else {
                    // use htmlspecialchars() to encode any html special characters cause never trust the user
                    printf('No student with ID "%s" found.', htmlspecialchars($idNumber));
                }
            }
        ?>
            <form action="" method="POST">
                Id number: <input type="text" name="search">
                <input type="submit" value="search">
            </form>
        </body>
    </html>
    

array_search()array_keys()sprintf()

的PHP文档

现在假设我们不需要有3个并行数组,我们可以将其切换到以下内容,以减少搜索次数

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
    <?php
        if (isset($_POST['search'])) {
            // note how the student id is now the key to that student's 
            // name and grade. This will allow us to access it more faster.
            $students = array (
                "F001" =>array("name"=>"albert"     ,"grade"=>1)
                ,"F002"=>array("name"=>"berto"      ,"grade"=>2)
                ,"F003"=>array("name"=>"charlie"    ,"grade"=>3)
                ,"F004"=>array("name"=>"david"      ,"grade"=>3)
                ,"F005"=>array("name"=>"earl"       ,"grade"=>2)
                ,"F006"=>array("name"=>"francis"    ,"grade"=>1)
                ,"F007"=>array("name"=>"garry"      ,"grade"=>2)
                ,"F008"=>array("name"=>"harry"      ,"grade"=>1)
                ,"F009"=>array("name"=>"irish"      ,"grade"=>3)
                ,"F010"=>array("name"=>"james"      ,"grade"=>1)
            );

            $idNumber = $_POST['search'];
            // since we set up our array the way we did, we know that: if
            // the key is found, there will be a name and grade associated
            if (isset($students[$idNumber])) {
                printf('Student ID: %s<br>Name: %s<br>Grade: %d', $idNumber, $students[$idNumber]['name'], $students[$idNumber]['grade']);
            }
            else {
                printf('No student with ID "%s" found.', htmlspecialchars($idNumber));
            }
        }
    ?>
        <form action="" method="POST">
            Id number: <input type="text" name="search">
            <input type="submit" value="search">
        </form>
    </body>
</html>

* it helps if you play this