如何在PHP中访问特定的数组索引并显示它?
所以在这个项目中,我有 3 个 PHP 文件,即index.php、processor.php和viewsubmission.php
我的index.php和processor.php两者都按我的预期工作得很好,我现在的问题是在viewsubmission.php.
当我运行这个文件时,它只显示:
有没有一种可能的方法可以在来自文本文件的数据之前显示文本?预期的输出应该是这样的。
第二个问题:如何显示新数据而不必删除正在显示的第一组数据。
index.php //包含我的表单
<form action = "Processor.php" method = "post" enctype="multipart/form-data">
Student ID:<input type="text" name="studentId"><br>
Student Name: <input type="text" name="studentName"><br>
Activity Number: <input type="text" name="activityNumber"><br>
Comment: <input type="text" name="comment"><br>
File Upload (doc,docx, & pdf files only):<br><input type="file" name="fileupload"> <br><br>
<button type="submit">Submit File</button>
</form>
Processor.php //对表单中的所有输入进行编码处理然后写入文本文件
//creating the name of the file
$newFileName = uniqid('',true) . "." . $FileExt;
//identify file destination
$fileDst = "uploads/" . $newFileName;
move_uploaded_file($TempFile, $fileDst);
//Write data in textfile
$file = fopen("uploads/data.txt", "a");
fwrite($file, $_POST['studentId'] . "\n");
fwrite($file, $_POST['studentName'] . "\n");
fwrite($file, $_POST['activityNumber'] . "\n");
fwrite($file, $_POST['comment'] . "\n");
fwrite($file, $fileDst . "\n");
fclose($file);
echo "<br><br>>New file Location: " . $fileDst . "<br>";
echo "Record Saved...";
viewsubmission.php //访问文件并读取其内容并显示
<?php
$filePath = "uploads/data.txt";
$content = file_get_contents($filePath);
$values = explode("\n", $content);
echo "<hr>";
$numberofData = 0;
foreach($values as $val){
echo $val . "<br>";
$numberofData++;
}
$numberofData--;
echo "<hr>";