Page 1 of 1

Read in csv headers, allow client to choose what goes where

Posted: Mon Jan 19, 2009 11:00 am
by webman2015
I am working on a program to read in a csv file and modify it to fit certain criteria. I am new to php, so I am not very good at making the visible interface. How can I dynamically read in the headers from a csv file - lets say 45 columns and allow a client to assign them to about 25 columns in the final output file? Of course, only 25 of the original 45 columns will even be used, the others will be ignored. The file to be modified will also be labeled differently from the labels we want to use.

For example, I want to read in a txt file with the 25 headers we want to use and display them on the screen along with the column number. Such as:
First Name - 1, Last Name - 2, Address - 3, ZIP - 4, Email - 5..... Tel - 25

Next, I read in the csv file headers from the data we want to format and display them as such with an input box next to each:
tel# 1 [], fax# 2 [], zipcode 3 [], email 4 [], residence 5 []..... firstname 45 []

Then I want the client to be able to point out which of the 45 items goes where by entering the appropriate number next to each. But the 25 final headers can each only be used one time.

I know how to read in the txt file and display it on the screen. But I do not know how to read in the CSV file dynamically and assign it to the proper places as needed using a form.

I can read in a csv file line by line and assign it through programming to each appropriate header manually, but that is very time consuming and I have to write a new file for each and every spreadsheet I read in. Literally every spreadsheet is different than the last. And I am dealing with hundreds because we are taking in data from many sources. I just cant figure out how to do this dynamically.

Any help would be greatly appreciated.

Re: Read in csv headers, allow client to choose what goes where

Posted: Mon Jan 19, 2009 11:10 am
by pickle
Basically what the client will be doing is creating a mapping for you that maps 25 of the 45 columns in the file, to the 25 columns you want. So if you had a numerical array keyed from 0-24 (or 1-25) if you want, you can put the number (0-44 or 1-45) of the user chosen column as the associated value to each key.

Once you've got the array built and you want to display the file, just read in the whole file (fgetcsv() could be helpful here) and only pull out the columns specified as values in your array.

Re: Read in csv headers, allow client to choose what goes where

Posted: Mon Jan 19, 2009 11:23 am
by webman2015
Yes, that's exactly right. But I dont know how to set up the form to only allow each of the numbers from 1-25 to be used once.

I have to admit, I am not good at working with forms yet either.

Re: Read in csv headers, allow client to choose what goes where

Posted: Mon Jan 19, 2009 11:36 am
by pickle
Oh right - forgot to speak to that.

I'd recommend rather than having the users type in numbers, have then choose options from select boxes. Once an option is chosen in one select box, use Javascript to remove that option from the other select boxes. Name each select box something like: "choices[]", then when the form gets submitted, $_POST['choices'] will be an array of the user's choices.

Server-side (since you don't want to trust data from the user), just run $_POST['choices'] through array_unique() and count(). If the count() of $_POST['choices'] is the same length as the result of running it through array_unique(), then you know no duplicates were entered.

Re: Read in csv headers, allow client to choose what goes where

Posted: Mon Jan 19, 2009 1:08 pm
by webman2015
Thanks, I will try this.

Re: Read in csv headers, allow client to choose what goes where

Posted: Mon Jan 19, 2009 3:14 pm
by webman2015
How do I set up a form to read the customer's input into an array?

If, for example, there are 45 columns in my spreadsheet, I will need a form with an array of 45 lines.

Re: Read in csv headers, allow client to choose what goes where

Posted: Mon Jan 19, 2009 3:33 pm
by pickle
Check the second paragraph of my first post.