Re: Tricky explode(), help please!



torpecool@xxxxxxxxx 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
.



Relevant Pages

  • Re: Check if any items in an array (php) is in a table (mysql).
    ... I want to check the ship record (a table in a MySQL database) to check ... I could brute force it and increment through the "to be shipped array" ... Load the shipped table as an associative array and then do an isseton the $arrayin question. ...
    (comp.lang.php)
  • Tricky explode(), help please!
    ... I am trying to parse and load a tab delimited file into a mysql ... When I load this information into the database, ... explodefunction to break this array down into its parts, ...
    (comp.lang.php)
  • Re: System.Drawing.Image.FromFile leaves file open longer than necessary: bug?
    ... load on the system when it finally does it by itself. ... > dispose the stream as soon as you are done reading it. ... database. ... stream out of the same array for copying into the picturebox, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: SQL Server, MS Word and C#.
    ... You should be able to load your document into an array, ... > I want to insert some word files into the database and also be able to> retreive them. ... > 1) What DataType should the fiel be in the DB. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: KirbyBase
    ... creating objects from the database records was much easier. ... Hal, I don't know if you have had a chance to take a look at the beta yet, but I basically tried to implement a uniform way to specify one-to-one links, one-to-many links, and calculated fields in the ... I suppose it would in effect be embedding an array where all the ... My first couple of attempts at adding more complexity to KirbyBase did not honor this concept. ...
    (comp.lang.ruby)