我知道在这里一般不接受家庭作业的帮助,但我已经被困了一个多星期了,所以我会非常感激任何帮助。它在概念上似乎很简单,但我无法从爆炸文件到将数据转换为数组。
目标是有一些东西来比较从表单输入的文本。例如,如果在名称字段中键入名称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 :(得分: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"
?>
答案 1 :(得分:0)
您的代码看起来可以正常工作。
部分出了什么问题:
$testarray[] = array('$stoodname');
当您将变量放在单引号中时 - 它将被视为字符串。
尝试
$testarray[] = $stoodname;
// other dirt
$studentExists = in_array($stoodname, $testarray);
if($studentExist) {
// code
}