Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a PHP Expert!

Join 244,301 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 784 people online right now. Registration is fast and FREE... Join Now!




PHP script reading duplicate files in .csv

 
Reply to this topicStart new topic

PHP script reading duplicate files in .csv, won't forward until ALL matches are found

midasxl
5 Jan, 2009 - 04:25 PM
Post #1

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 105



Thanked: 1 times
My Contributions
OK, I have this great script which works great. The script takes the user supplied first name and id number and uses it to find a match in the .csv file. If a match is found, user is forwarded to a specific URL. Thanks to Martyr2 for the awesome help with this one.

What I have run into is that in the .csv file there are duplicates, and sometimes three or more. So the script is finding 2, 3, and sometimes 4 matches. The problem is it will not forward the user until ALL matches are found. So a name/id combination that shows up 4 times in the .csv will force the user to enter his/her name/id in the form 4 times! Strange huh? Any way around this little anomaly in the code? I've been told the .csv cannot strip duplicates because of the type of program that creates it. Thanks for any ideas!

CODE

<form name="input" method="post"
action="<?php echo $_SERVER['php_test/SCRIPT_NAME']?>">

<p><label for="firstname">First Name: </label>
<input type="text" name="fname" id="fname" /></p>

<p><label for="idnumber">ID Number: </label>
<input type="text" name="idnumber" id="idnumber" /></p>

<p><input type="reset" name="reset" value="reset" />
<input type="submit" name="submit" value="submit" /></p>
</form>

<?php

if (isset($_POST['submit'])) {

$fname = $_POST['fname'];
$idnum = $_POST['idnumber'];
$array = file("test.csv");
$match_found = false;
foreach ($array as $line) {
$pieces = explode(",",$line);
if ((trim($pieces[0]) == $fname) && (trim($pieces[2]) == $idnum)) {
$match_found = true; break;
}
}
if($match_found){
echo 'You are signed in!';
$URL="desired URL";

header ("Location: $URL");

} else {
echo 'nope';
}
}
?>


User is offlineProfile CardPM
+Quote Post


CTphpnwb
RE: PHP Script Reading Duplicate Files In .csv
5 Jan, 2009 - 05:31 PM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
It doesn't look to me like it's finding multiple matches. In fact, it should stop after finding the first one. That would force the user to enter their information multiple times to get into multiple URLs.


User is offlineProfile CardPM
+Quote Post

midasxl
RE: PHP Script Reading Duplicate Files In .csv
5 Jan, 2009 - 06:44 PM
Post #3

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 105



Thanked: 1 times
My Contributions
QUOTE(CTphpnwb @ 5 Jan, 2009 - 05:31 PM) *

It doesn't look to me like it's finding multiple matches. In fact, it should stop after finding the first one. That would force the user to enter their information multiple times to get into multiple URLs.


I hear you on that one. It doesn't make sense to me either. According to the code, as soon as it finds a match it should break and move on. I have tested and retested the script against the .csv file and it does the same thing everytime. However many matches it finds, that's how many times I have to enter the name/id in order to get the forward to kick in.

Very strange.
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: PHP Script Reading Duplicate Files In .csv
5 Jan, 2009 - 06:59 PM
Post #4

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
What does the csv file look like? Because the array references items 0 and 2, it should have something in between the name and the id:

first name,some value,ID Number
User is offlineProfile CardPM
+Quote Post

midasxl
RE: PHP Script Reading Duplicate Files In .csv
6 Jan, 2009 - 08:11 AM
Post #5

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 105



Thanked: 1 times
My Contributions
QUOTE(CTphpnwb @ 5 Jan, 2009 - 06:59 PM) *

What does the csv file look like? Because the array references items 0 and 2, it should have something in between the name and the id:

first name,some value,ID Number


The value in between is a birthdate (00/00/0000)

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: PHP Script Reading Duplicate Files In .csv
6 Jan, 2009 - 09:12 AM
Post #6

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
How about if you add exit(); after the redirect?
User is offlineProfile CardPM
+Quote Post

midasxl
RE: PHP Script Reading Duplicate Files In .csv
7 Jan, 2009 - 09:04 AM
Post #7

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 105



Thanked: 1 times
My Contributions
Ok, I ended up adding a little script that eliminates duplicates from the .csv file, thereby eliminating my login issue.

CODE

<?php
$filename = "withDuplicates.csv";
$file = fopen($filename, "r");
$read = fread($file, filesize($filename));

$split = array_unique(explode("\n", $read));

fclose($file);

$filename2 = "noDuplicates.csv";

$file2 = fopen($filename2, "w");

foreach($split as $key=>$value) {
    if($value != "") {
        fwrite($file2, $value . "\n");
    }
}

fclose($file2);

echo "Duplicates have been removed!  Thanks for playing!";
?>

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:32PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month