我已經創建了一個將數據傳遞到文本文件並將文本保存到數組上的數據的HTML表單。檢查數組中的重複
我無法弄清楚如何在數組或文本文件中輸入另一個輸入之前檢查值是否已經存在。
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
$myArray = array();
if (isset($_POST['save']))
{
/*The file will be created in the same directory where the PHP code resides
*a+ to not overwrite text*/
$myfile = fopen("DonorList.txt", "a+") or die("Unable to open file!");
//get data entered by user from the form
fwrite($myfile, $_POST['name']);
fwrite($myfile, " ");
fwrite($myfile, $_POST['email']);
fwrite($myfile, "\r\n"); //next line
fclose($myfile);//close file
textOutput();//call function
}
print_r($myArray);
//creating function to make code more readable
function textOutput()
{
$lines = file('DonorList.txt');
foreach ($lines as $lines_num => $line)
{
echo "User Input: ".htmlspecialchars($line)."<br />\n";
}
$file = fopen("DonorList.txt", "r");
while(!feof($file))
{
$myArray[]= fgets($file);
}
fclose($file);
}
?>
打開文件進行閱讀。將值讀回不同的數組並比較每個值? – jmunsch