2012-07-12 94 views
0

我知道帮助作业一般不会在这里被接受,但我已经被卡住了一个多星期了,所以我会非常感激任何帮助。它在概念上似乎很简单,但我无法从爆炸文件中获取该信息到数组中。来自列表数据的PHP数组

目标是有东西来比较从窗体输入的文本。例如,如果名称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" 
?> 

我觉得我只是缺少明显的东西:(

+0

只注意到你想呼应的阵列 - 尝试的print_r($ testarray);而不是回声; – TigerTiger 2012-07-12 09:15:13

+0

它给了我一个巨大的列表“Array([0] => Array([0] => $ standname)” – WizardOfLoss 2012-07-12 09:17:29

+1

并获取文件内容到数组中 - 尝试file() - http://php.net/ manual/en/function.file.php – TigerTiger 2012-07-12 09:17:35

回答

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" 
?> 
+0

学生文件是由CSV单独分隔的,但有学生名称和学号,这有点帮助虽然! – WizardOfLoss 2012-07-12 18:55:47

0

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

什么是部分错误:

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

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

尝试

$testarray[] = $stoodname; 

// other dirt 

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

if($studentExist) { 
    // code 
}