I have a master list populated from a master.csv file then I have multiple (10+) list populated from list1.csv, list2.csv, list3.csv …etc. I want to move items from masterlist to list1 but need to check that item is not in any of the lists before it can be moved Herewith code but only use 3 list but there is more but I am unable to check for duplicates before move
Your statement of what you are trying to do, combined with the example data, lacks clarity. I also reviewed your most recent posts and do these csv files actually contain lines of csv data or are they really just single values on each line in the file?
I’m reading this as - since January, February, March, and April are already picked and stored in the output files, they are unavailable to be picked when this page is displayed, any of the remaining available choices can be added to any output file, and any choice made while on this ‘edit’ page should ‘dynamically’ become unavailable in all the select/option menus?
Next, don’t write out block after block of the same program logic that only differs in the data values it operates on. Let the computer generate the html markup and process the submitted data for the 10 possible files by looping to produce the html markup and looping to dynamically process the 10 sets of data from the form.
Then you don’t need all the extra code treating it as csv data. Just use file(), with the FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES flags, to get all the lines from any file into an array.
You would read the contents of all the files, then if a choice exists in one of the 10+ output files, either leave the choice(s) out of the master select/option list or you would add the disabled attribute.
How do you know how many output files there are? Do you have some type of ‘add’ menu choice to add an empty one?
No one stated to do anything to the master.csv file. Once you read the contents of the file into memory, you can do anything you want to that copy of the data.
Because you have used a separate form for each output file, I’ll assume that you want to update one output file at a time, then go onto the next? This makes the overall logic simpler since you won’t need to ‘dynamically’ remove choices using javascript in the browser as you make sections and restore those choices if you remove a selection.
The logic to read the existing data needs to be all together and before the start of the html document -
// get method business logic - get/produce data needed to display the page
$num_files = 10; // if this is dependent on some 'dynamic' value, change the logic for this value get the actual number of files to handle
// read existing data
$master = file('master.csv',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$list = []; // individual file contents
$all = []; // all existing selections merged into one array
foreach(range(1,$num_files) as $num)
{
$file = "list{$num}.csv"; // form the file name
$list[$num] = []; // initialize to an empty array
if(file_exists($file))
{
$list[$num] = file($file,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$all = array_merge($all,$list[$num]);
}
}
At the point of producing the master select/option menu, this is all the logic you need -
The $list variable is an array of arrays, with the main index being the output file number 1…10 or whatever the maximum is. You would loop over this array to get the file number and the array of existing values that were read from that corresponding file, to produce the select/option menus in the rest of the code.
Thanx works 100% just want to test something when uploading item to master list what influence it will have will get back to you if any problems Just need to quickly run to store for groceries
OK have done my testing uploading item and everything is working 100% Just want to thank you I was spending 2 days looking at various ways to accomplish.
What processing you are going to do based on all possible values of those inputs.
What result or output are you going to produce.
What I came up with for those -
A list number/id (integer.) An array of choices (strings). Note: If the master choices were defined/stored in a database table, the submitted choices would be integer ids instead of strings.
There must be a non-empty list number/id with a value between one and the maximum permitted/defined value.
To allow for ‘clearing’ a list, the array of choices may not exist (a select/option menu that has nothing selected isn’t a successful form field and isn’t included in the form data.)
If there is an array of choices, remove empty values (a value that’s set but is an empty string/zero), remove duplicates, and sort the remaining values.
Save the choices to a file - list{id}.csv, one line per choice. If clearing the list, save an empty file.
Note: To allow for clearing a list, the list number/id must be a separate form field from the select/option menu, since the select/option field won’t be submitted in this case. A hidden field should be used for the list number/id. This would require adding a hidden field to your existing form(s) for the list number/id, then remove the list number/id from the end of the select field name attribute.