I have the following incoming txt file:
H header1 yyyy
I detailofheader1
I detailofheader1
H header2 xxxx
I detailofheader2
I detailofheader2
And the following code:
$action = substr($line_of_text, 0, 1);
if (($action == 'H') ) {
//STORE IN VARIABLES ;
}
if (($action == 'I') ) {
//store in variables and upload header and detail variables to mysql;
}
I would like to have a loop read first header store in variable then read details and everytime it hits 'I' it uploads that line to MYSQL.
How do i go back to the top of the loop once it hits H again?
Thanks from a noobie.
There are multiple ways to parse your file.
The following approach makes use of
explode()
for H and I.So you have a "outer loop" for H (
H header1 yyyy I detailofheader1 I detailofheader1
) - to process the individual headers.And an "inner loop" for I (
header1 yyyy I detailofheader1 I detailofheader1
) - to process the header data itself and the details.SQL-wise:
echo $sql
- you could exec the query.Anyway, here's something to play around with. Hope that gets you started...