Question

THE PROCESS:

  1. User checks checkboxes to share files with customer accounts
  2. Checkbox values are compared against an array stored in a txt file from the customers folder
  3. The arrays are compared by being merged into one array using array_merge()
  4. The duplicates are eliminated using array_unique()
  5. New array written to txt file

THE PROBLEM:

If my text file already contains the following data: (numbers representing text file lines)

  1. M HTH A277 Frame Off STD Specs 02-01-12.pdf
  2. M HTH A277 Frame On STD Specs 02-01-12.pdf
  3. M HTH Option Can Price List 02-02-2012.xls

I then try to share more files including those that are already shared. My new text file looks like this: (numbers representing text file lines)

  1. M HTH A277 Frame Off STD Specs 02-01-12.pdf
  2. (blank)
  3. M HTH A277 Frame On STD Specs 02-01-12.pdf
  4. (blank)
  5. M HTH Option Can Price List 02-02-2012.xls
  6. (blank)
  7. (blank)
  8. M HTH A277 Frame Off STD Specs 02-01-12.pdf
  9. M HTH A277 Frame On STD Specs 02-01-12.pdf
  10. M HTH Option Can Price List 02-02-2012.xls
  11. Valley Creek Estates - 2010.pdf

The values above are the exact values I'm dealing with. I've tried to be as thorough as possible with this explanation which could make things confusing. If anyone can provide me with any suggestions they would be greatly appreciated. Thanks in advance. This is what I've got for code so far:

THE CODE:

$arr = $_POST['checked'];
$cust = $_POST['custname'];

    if ($cust != ""){

        $myFile = "CONTA.txt";

        //If file exists get previous array from file
        if (file_exists("customer/" . $cust . "/" . $myFile)) {         

            $fh = fopen("customer/" . $cust . "/" . $myFile, 'r') or die("");

                while (!feof($fh) ) {
                        $compare[] = fgets($fh);
                }

            fclose($fh);

            //Combine checkbox array with previous array & eliminate duplicates

            $combined = array_unique(array_merge($compare,$arr));

        }
        else
        {

            //Since no previous file or array existed. Just use current checkbox array.     
            $combined = $arr;


        }

        //Input array into file
        $fh = fopen("customer/" . $cust . "/" . $myFile, 'w') or die("can't open file");

            foreach ($combined as $value) {

                    fwrite($fh, $value . "\n");

            }
        echo "<span class='message'>Items shared successfully!</span>";
        fclose($fh);
    }

}
Was it helpful?

Solution

To me it looks like the problem is "\n" character. Each line has a new line on the end and when you compare one line that has the newline character and the same line that doesn't they aren't the same. I would confirm this by echoing each line from the fgets. If they are line broken, then you know you are getting the new line character.

EDIT:
I would try putting
trim(fgets($fh))
by default it should strip the newline character
trim specs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top