[英]Lookup in CSV file is failing
I have a form where the user enters their first and last names. 我有一个表单,用户可以在其中输入名字和姓氏。 I want to check if their full name is present in my CSV file, which I use to store all current members / users of my form.
我想检查他们的全名是否存在于我的CSV文件中,该文件用于存储表单中的所有当前成员/用户。 The format in which data is stored is
firstname,lastname
. 存储数据的格式为
firstname,lastname
。 And if they are present, I want to print out You are already a member
. 如果有的话,我想打印出
You are already a member
。
But that is not what I see in output, when I run my code. 但这不是我在运行代码时在输出中看到的。 It doesn't output that, even if I input names which are present in the CSV file.
即使输入了CSV文件中存在的名称,它也不会输出。
Here is my source code: 这是我的源代码:
$JLMembers = Array();
// Open the file of Members
if ($handle = fopen("JLMembers.csv", "r")) {
// Get the next CSV line
while ($data = fgetcsv($handle, 1000, ",")) {
// Add that line to the array
array_push($JLMembers, $data);
}
}
foreach($JLMembers as $JLMember) {
$FirstName = $_GET['FirstName'];
$CurrentMemberFirstName = $JLMembers[0];
if ($FirstName === $CurrentMemberFirstName) {
foreach($JLMembers as $JLMember) {
$SecondName = $_GET['LastName'];
$CurrentMemberSecondName = $JLMembers[1];
if ($SecondName === $CurrentMemberSecondName) {
print "<br>You are already a member<br>";
}
}
}
}
You're using $JLMembers[0]
, which contains the first row of the CSV, when you should be using $JLMember[0]
, which contains the first column of the current row in the foreach
loop. 您应使用
$JLMembers[0]
,它包含CSV的第一行,而您应使用$JLMember[0]
,它包含foreach
循环中当前行的第一列。 And you don't need two loops, just compare both columns in one loop. 而且您不需要两个循环,只需在一个循环中比较两列即可。
The code should be: 代码应为:
$FirstName = $_GET['FirstName'];
$LastName = $_GET['LastName'];
foreach ($JLMembers as $JLMember) {
if ($FirstName == $JLMember[0] && $LastName == $JLMember[1]) {
echo "<br>You are a member<br>";
break; // Exit the loop once we found a match
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.