Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用文本文件_Php_Text Files - Fatal编程技术网

在PHP中使用文本文件

在PHP中使用文本文件,php,text-files,Php,Text Files,如前所述,我是PHP编码的初学者。我制作了一个文本文件,其中包含我的学生信息,如下所示-包括他们的姓名、科目分数、电子邮件地址和注册日期: 安·汤普森:50,90,82,64,75:ann@amuniversity.com:2016-02-01 耶利米·汉森:80、75、88:jeremiah@amuniversity.com:2016-03-02 比利·琼斯:89、72、46、54:billy@amuniversity.com:2016-04-12 我还制作了一个表单,输入一个字段,该字段接受

如前所述,我是PHP编码的初学者。我制作了一个文本文件,其中包含我的学生信息,如下所示-包括他们的姓名、科目分数、电子邮件地址和注册日期:

安·汤普森:50,90,82,64,75:ann@amuniversity.com:2016-02-01

耶利米·汉森:80、75、88:jeremiah@amuniversity.com:2016-03-02

比利·琼斯:89、72、46、54:billy@amuniversity.com:2016-04-12

我还制作了一个表单,输入一个字段,该字段接受用户希望读取的文件名,以及一个用于用户希望写入的文件名的字段

我从哪里开始获得下面的输出

学生总结:

  • Ann Thompson于2016-02-01注册,平均年龄为48岁,符号为F

  • 耶利米·汉森报名参加

  • 比利·琼斯报名参加

它还应该在底部显示上次修改的日期


请帮忙

请检查以下代码。我在我认为需要的地方对代码进行了评论。如果你不明白,请告诉我。显然,您需要根据您的评分系统更新checkGrade函数,但您得到了这样的想法:)


处理文本文件需要大量的工作;你为什么不使用数据库,或者这是一个练习?应该在数据库中-你真的打算在大学服务器上留下包含学生成绩和电子邮件地址的未加密文本文件吗?我给他们15分钟的时间,如果你这样做的话,他们就都是优秀学生了。@Fred ii-正如我所说的,我对PHP编码是新手。我首先尝试解决如何使用文本文件进行测试。根据经验,@Zera文本文件需要大量的工作,并且可能存在许多潜在的问题,而且数量众多。其中之一是,;一旦一个文本文件被以任何方式修改,就不能再返回了,除非你制定了复制它/它们的规定。最佳方式;使用数据库,这将使检索在某些方面更加容易和快速。您也可以将它们与数据库一起使用并导出,这也是一个选项。@Zera如果您坚持,那么您所要做的就是执行
fread()
并将这些数据拆分为各自的位。这可能不是最方便的,但它会得到你想要的。很乐意帮忙,没问题^_^
<?php
// Read the input file line by line into "input" array
$filename = "input.txt";
$input = explode("\n", file_get_contents($filename));

// Go through each line
for($i = 0; $i < sizeOf($input); $i++){

    // split line by :
    $line = explode(":", $input[$i]);

    // Initialize variables based on position
    $name = $line[0];
    $numbers = $line[1];
    $email = $line[2];
    $registration = $line[3];

    // Calculate average
    // Remove any spaces first from the numbers
    $numbers = str_replace(' ', '', $numbers);
    // Split the numbers by ,
    $numbersArray = explode(",", $numbers);
    $sum = 0;
    // Calculate sum
    for($j = 0; $j < sizeOf($numbersArray); $j++){
        $num = (int)$numbersArray[$j];
        $sum += $num;
    }
    $avg = $sum/sizeof($numbersArray);

    // Print it
    echo "$name enrolled in $registration, has an average of $avg, with a symbol ". checkGrade($avg) ."<br>";

}

// Last modified date of the input file
echo "<br><br><br>";
if (file_exists($filename)) {
    echo "This file was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}

function checkGrade($num){
    if ($num < 65){
        $grad = 'F';
    }
    else if ($num<= 66 && $num >=65){
        $grad = 'D';
    }
    else if ($num <= 69 && $num >=67){
        $grad = 'D+'; 
    }
    else if ($num <= 73 && $num >=70){
        $grad = 'C-';
    }
    else if ($num <= 76 && $num >=74){
        $grad = 'C';
    }
    else if ($num<= 79 && $num >=77 ){
        $grad = 'C+';
    }
    else if ($num <= 83 && $num >=80){
        $grad = 'B-'; 
    }
    else if ($num <= 86 && $num >=84){
        $grad = 'B';
    }
    else if ($num <= 89 && $num >=87){
        $grad = 'B+';
    }
    else if ($num <= 93 && $num >=90){
        $grad = 'A-';
    }
    else if ($num <= 96 && $num >=94){
        $grad = 'A';
    }
    else if ($num >= 97){
        $grad = 'A+';
    }
    return $grad;
}
?>
Ann Thompson enrolled in 2016-02-01, has an average of 72.2, with a symbol C-
Jeremiah Hanson enrolled in 2016-03-02, has an average of 81, with a symbol B-
Billy Jones enrolled in 2016-04-12, has an average of 65.25, with a symbol D



This file was last modified: May 03 2017 13:12:13.