0

我知道这里一般不接受帮助做作业,但我已经被困了一个多星期了,所以我会非常感谢任何帮助。概念上似乎很简单,但我无法从分解文件到将该信息放入数组中。

目标是有一些东西来比较从表单输入的文本。例如,如果名称 Bob 被输入到名称字段中,那么将有一种方法可以查看 Bob 的名称是否已经在列表中。

这是我最近的尝试:

<?php
    if(isset($_POST['sname'],$_POST['snumber'],$_POST['courseselect']))
    {
        //Has to be submitted first
        $studentname = $_POST['sname'];
        $studentnumber = $_POST['snumber'];
        $course = $_POST['courseselect'];
        if(empty($studentname) || empty($studentnumber))
        {
            //make sure all fields are filled in
            echo "All fields must be filled in";//for if they aren't filled in
        }
        else 
        {
            //if they are filled in
            $studentfile = "students";
            $SF = fopen($studentfile, 'r') or die("$studentfile cannot be opened for reading");

            while($studentlist = fgets($SF))
            {
                list ($stoodname, $stoodnumber) = explode('::', $studentlist);
                $testarray[] = array('$stoodname');
                echo $testarray;
            }
        }
        //end of if/else
    }//end of "has to be submitted"
?>

我觉得我只是错过了一些明显的东西:(

4

2 回答 2

0

假设您的学生文件是 CSV,::字段之间有:

<?php
    if(isset($_POST['sname'],$_POST['snumber'],$_POST['courseselect']))
    {
        //Has to be submitted first
        $studentname = $_POST['sname'];
        $studentnumber = $_POST['snumber'];
        $course = $_POST['courseselect'];
        if(empty($studentname) || empty($studentnumber))
        {
            //make sure all fields are filled in
            echo "All fields must be filled in";//for if they aren't filled in
        }
        else 
        {
            //if they are filled in
            $studentfile = "students";
            $SF = fopen($studentfile, 'r') or die("$studentfile cannot be opened for reading");

            while($studentlist = fgets($SF))
            {
                $testArray=explode('::', $studentlist);
                for($i=0;$<count($testArray);$i++)
                {
                    if($testArray[$i]==$studentname)
                    {
                        // You found your match!
                    }
                }
                print_r($testArray);
                unset($testArray);
            }
        }
        //end of if/else
    }//end of "has to be submitted"
?>
于 2012-07-12T09:30:08.623 回答
0

您的代码看起来可以工作。

有什么不对的部分:

$testarray[] = array('$stoodname');

当您将变量放在单引号中时 - 它将被视为字符串。

尝试

$testarray[] = $stoodname;

// other dirt

$studentExists = in_array($stoodname, $testarray);

if($studentExist) {
    //  code
}
于 2012-07-12T13:30:53.123 回答