|
Posted by noone on 10/23/33 11:40
torpecool@yahoo.com wrote:
> Hello Everyone,
>
> I have a situation that I just cannot wrap my head around. I would
> appreciate any ideas or suggestions.
>
> I am trying to parse and load a tab delimited file into a mysql
> database. The file is generated by a scientific instrument. One file
> contains the records of several people. Each person's information is
> in 3 consecutive lines of the file.
>
> A simplified demonstration:
>
> id | test | result
> 1 | test1 | 50
> | test 2 | 55
> | test 3 | 60
> 2 | test 1 | 10
> | test 2 | 15
> | test 3 | 20
> etc.
>
> When I load this information into the database, it should have this
> format
> id | test 1 | test 2 | test 3
> 1 | 50 | 55 | 60
> 2 | 10 | 15 | 20
>
> I'm okay with setting up the database, that's no problem.
>
> I am however rather new to PHP and although I know I have to use the
> explode() function to break this array down into its parts, I'm not
> exactly sure how. Specifically, one file could contain the records of
> up to 20 people, and I am not sure how I can loop through this file
> (loaded into an array) in order to capture all of this info.
>
> I hope I've given enough information to make sense. Again, any ideas,
> examples or suggestions would be appreciated.
>
> Thanks,
> Andy
>
Unless you know for sure that you will ALWAYS only have 3 tests then
your table design is flawed. If there can be more or less tests then
you want to use something like:
testid,testnum,testresult
It would take much less programming to insert this kind of data and then
you can use this cool "pivot table" example to retrieve the data- which
is identical to what you are trying to do...
See: http://en.wikibooks.org/wiki/Programming:MySQL/Pivot_table
processing the array - NOT REAL SYNTAX - that is left to the programmer.
read file into array
process array
loopsstart
if ID is not emtpy or is null or = ''
(or whatever charaters are in the first position)
then
newid=row[0]
insertid=newid,insertestnum=row[1],result=row[2]
excute insert statement or write to an output
and load output file when all rows have been
processed
else
insertid=newid,insertestnum=row[1],result=row[2]
goto loopstart
[Back to original message]
|