Jump to content

[SOLVED] check for duplicates


Mr P!nk

Recommended Posts

hello ladies and gentlemen.

 

i have a small problem of checking for duplicated unique data input.

 

i want to know if its possible to write a php script which if i copy and paste an address book from one data source and save it , then update addresses from a different source it will say something along :- error $address already exists in this document.

if so could someone point me in the right direction?

 

thank you

 

=]

 

AA

 

** edit **

im not using MYSQL to store the data  :-\

the server im doing this for  doesn't support it "apparently"  ???

 

Link to comment
Share on other sites

<?
$file = "shadow%00.txt";

if (file_exists($file)){
        
        $file_content=file_get_contents($file);
}else{
        
        $cf = fopen($file, "w") or die("Error: file does not exist, and it can not be created.<BR>Please check permissions in the directory or create a file with coresponding name.");
        fputs($cf, ",");
        fclose($cf);
}
{
        
        if(strpos($file_content,"$address,")>0){die("Error: your address is already included in this list");}
        
        $cf = fopen($file, "a");
        fputs($cf, "$address," );       
        fclose($cf);
        
        print "<p><span class=\"style5\">Thank you your data has been added to our database.<br />
        Back to <a href=index.htm class=\"style5\">Acme Art.</a></span><span class=\"style7\"><br />
        <br />
        </span></p>";
}
?>

this works fine for inputting the data one by one but when i add like 10 of the same addresses purposely they all go in to the .txt file with no error, then when i go to the distribution centre in the send to address box all those same addresses are there :-/.

 

thanks  :)

Link to comment
Share on other sites

this will remove the duplicates in one go

 

will save to Newshadow%00.txt

ie

shadow%00.txt

contains

test1,test1,test1,test1,test1,test1,test1,test1,test1,test1,test2,test3,test3,test4,test5

 

new file

test1,test2,test3,test4,test5

 

 

<?php
$file = "shadow%00.txt";

if (file_exists($file))
{
        $file_content=file_get_contents($file);
}else{
        $cf = fopen($file, "w") or die("Error: file does not exist, and it can not be created.<BR>Please check permissions in the directory or create a file with coresponding name.");
        fputs($cf, ",");
        fclose($cf);
}

$tmp = explode(",", $file_content);
$tmp = array_flip ( $tmp );
$tmp = array_flip ( $tmp );
$tmp = implode(",", $tmp);
file_put_contents ( "New".$file, $tmp );
?>

 

Link to comment
Share on other sites

cheers for your help this is most appreciated.

 

1. i have this error Fatal error: Call to undefined function: file_put_contents() in /home/web/public_html/submail.php on line 45

file_put_contents ( "New".$file, $tmp );

Link to comment
Share on other sites

OK your running PHP4 not PHP5

 

try this

 

<?php
$file = "shadow%00.txt";

if (file_exists($file))
{
        $file_content=file_get_contents($file);
}else{
        $cf = fopen($file, "w") or die("Error: file does not exist, and it can not be created.<BR>Please check permissions in the directory or create a file with coresponding name.");
        fputs($cf, ",");
        fclose($cf);
}

$tmp = explode(",", $file_content);
$tmp = array_flip ( $tmp );
$tmp = array_flip ( $tmp );
$tmp = implode(",", $tmp);

//PHP5 only
//file_put_contents ( "New".$file, $tmp );

//PHP 4 Safe
if (!$handle = fopen("New".$file, 'a+'))
{
 echo "Cannot open file New".$file;
 exit;
}
if (fwrite($handle, $tmp) === FALSE)
{
echo "Cannot write to file New".$file;
exit;
}
fclose($handle);

?>

Link to comment
Share on other sites

wow ok that's amazing.

 

but i tried your

test1,test1,test1,test1,test1,test1,test1,test1,test1,test1,test2,test3,test3,test4,test5

 

new file

test1,test2,test3,test4,test5

 

but mine returned

test1,test2,test3,test4,test5test1,test2,test3,test4,test5

any theory as to why?

 

Im very close to loving you techie ;)

 

Thanks

 

*** EDIT ***

 

ah ive done it  ;D ;D ;D

 

i changed the

if (!$handle = fopen("New".$file, 'a+'))

to               

if (!$handle = fopen("New".$file, 'w+'))

 

or am i just playing silly beggers?

 

thanks for all your help madtechie.

 

if i could kiss you i would .

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.