0

Ok so now I am stuck, I looked at the http://www.daniweb.com/web-development/php/threads/326436/php-read-text-file-and-save-into-databse page and understand some of it, Yet it is not what i require, substring does not count like I thought it would, like the left, right mid commands in excel.

I want to count to the data I want and post it to MySql

Data looks like this' the fie is always in the same format
Let say the date is the 30th char from the left, I want to use something like:

$date = String or Substring( Distance to char, Amount of chars

Loop after line 21 untill left = "=" or ""

end loop

So it will look like extract just that section of data.

file attached

I have done this VB and it works, but now I finding it hard to get Just the date from line 1
Assign it to variable, Then go to Line 21
Assign the AccountNumber, Customer number, Sales Repname, Amount

Post to Mysql, The document Date, AccountNumber, Customer number, Sales Rep, Amount

then next ine 22, (We have the date so dont need to get it again)
Assign the AccountNumber, Customer number, Sales Repname, Amount .. and do this untill the begining of the line is either empty or has this "=" char

im stuck just on that

2
Contributors
4
Replies
12
Views
5 Years
Discussion Span
Last Post by branding4you
0

Hi, It seems after allot of hours messing around I have solved most of it, the last part Im a bit dumb for ... :)

I took a break and it hellped but the line numbers are still problematic.

<?php
$fp = fopen('tmp/file1.txt','r');

$loop = 0;

function left($str, $length) {
return substr($str, 0, $length);
}

function right($str, $length) {
return substr($str, -$length);

}

echo("<pre>");



while (!feof($fp)) {


//LINE 1: GO TO LINE 1 AND SELECT 10 MOST RIGHT CHARACTERS TO GET DATE

$date= trim(substr($line, 0, 600));
//echo left($date, 10); // LEFT
//echo '<br>';
//echo right($date, 10); // RIGHT
//echo '<br>';

//echo right($date, 10); // RIGHT DIGITS
$transactiondate = right($date, 10);

//GO TO LINE 21 IN TEXT FILE<br>
$line = '21';

//FROM HERE THE LOOP NEEDS TO WORK LIKE THIS
//READ LINES AFTER THIS LINE UNTILL THE 3d DIGIT IN THE NEXT LINE IS EMPTY

        //LINE 21 IS BROKEN UP INTO THE HEADERS IN TEXT FILE
        //substr ($thetext_toparse, $character_tostartat, $how_many_characters)


        //$sourcedocref = from char 3 to 10 - in other words only the 8 digit number
        $sourcedocref = substr($line, 3, 10)

        //$purchaseordernum = from char 36 to character 46
        $purchaseordernum = substr($line, 36, 46);

        //$paymentnum = from character 70 to 79 
        $paymentnum =  = substr($line, 70, 79);

        //$paymentuser = from character 93 to 106
        $paymentuser = substr($line, 93, 106);

        //$linevalue = from character 107 to 128
        $linevalue = substr($line, 107, 128);



        //POST LINE TO MYSQL INSERT INTO BLAH BLAH (date,source,purchase,payment,user,value) values ($date,$sourcedocref,$purchaseordernum ... etc etc etc 

//GO TO NEXT LINE
//IF 3d DIGIT IN THE LINE IS EMPTY THEN STOP LOOP

//DELETE TEXT FILE.

$loop++;

$line = fgets($fp,1024);

}

echo("</pre>");

?>

So my problem now only seems to be stuck with that loop, I have tried do while like in VB but im dumb with PHP.

How does one let PHP see if there is a number in the 3d char from right, and if it is let the $line++, if it is not, stop the loop and delete the text file

Thank you for the help

0

text file im reading, it wil stay in this format always

Attachments
BAS                                              DATE: 05/09/2012
20001                                                  NAT: The store                                            TIME: 17:48:43
RP007BS                                                    PAYMENT STUB                                          PAGE     6

 CONTACT PERSON          : I MOHLOPI                                               CONTACT TEL NO : 012 3005919
 INSTALLATION DESCRIPTION: NAT: The store                                          DISBURSEMENT NO: 000022422
 LOCATION DESCRIPTION    : NAT: The store                                          ACTION DATE    : 10/09/2012


 PAYEE NAME     : IMPRESIVE GROUP (PTY) LTD                                        BANK NAME   : FIRSTDOLLAR BANK LIMITED
 PAYMENT ADDRESS: 16 ERNEST OPPENHEIMER AVENUE                                     BANK BRANCH : CORP CORE BANKING/GTS JHB
                  BRUMA                                                            ACCOUNT TYPE: CURRENT
                  JOHANNESBURG                                                     ACCOUNT NO  : 99491182664

                  2198


  SOURCE DOC                        PURCHASE ORDER                    PAYMENT    FUNCTIONAL
  NUMBER                            NUMBER                            NUMBER     AREA        USER          AMOUNT
 ---------------------------------------------------------------------------------------------------------------------------------
  21860777                          NOT APPLIC                        000050021  AP          SHARON                     4,050.00
  22031057                          NOT APPLIC                        000049905  AP          ELISA                        533.00
  22000444                          NOT APPLIC                        000049900  AP          ELISA                        533.00
  21986364                          NOT APPLIC                        000049975  AP          ELISA                        460.00
                                                                                                          ------------------------
                                                                                             TOTAL                      5,576.00
                                                                                                          ------------------------
PLEASE REPORT IF RECEIVED BY MISTAKE OR IF YOU DETECT ANY FRAUD.
                                                  **** END OF REPORT RP007BS ****
0

GOT IT !! YAY! ... lol

Some guidance from other pages on Daniweb, combining code and a LOT of trail and error.

Did not want to post anything here that day but I was stuck - I did however keep trying and trying ... the result was a text file reader that posts the file contents to MYSQL, next will be delete the file I got the info from, that will be easy. For anyone out there who searches for this - here is my code, it took me a few days being a newbie to get this to work.

Enjoy my code!

<?php

    include_once "db_conf.php";

    //THIS WILL BE LINKED TO MYSQL AFTER UPLAOD I WILL SEARCH FOR FILENAME, THE NAME HERE IS JUST HARD CODED

    $fp = fopen('tmp/file1.txt','r');
        if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

        //FUNCTIONS

        function left($str, $length) {
        return substr($str, 0, $length);
        }

        function right($str, $length) {
        return substr($str, -$length);

        }

        $loop = 0;

        echo("<pre>");

        while (!feof($fp)) {

                //GET LINE DATA
                $loop++;


                //echo "line:$loop:";
                $line = fgets($fp,1024);
                $linedata= trim(substr($line, 0, 600));

    //GET LINE 1 DATA
        if ($loop=="1")
            {
            echo right($linedata, 10); //DATE
            echo "<br>";
            }



        if ($loop >= "21")
            {
              $i= substr($linedata, 1, 1 );
                if ($i>=1) 

                 {

                  //========================================

                    //$sourcedocref = from char 2 number of chars 8
                    $sourcedocref = substr($line, 2, 8);
                    echo $sourcedocref;
                    echo",";    

                    //$purchaseordernum = from char 36 number of chars 10
                    $purchaseordernum = substr($line, 36, 10);
                    echo $purchaseordernum;
                    echo",";

                    //$paymentnum = from character 70 number of chars 10 
                    $paymentnum = trim(substr($line, 70, 10));
                    echo $paymentnum;
                    echo",";

                    //$paymentuser = from character 93 number of chars 10
                    $paymentuser =  trim(substr($line, 93, 10));
                    echo $paymentuser;
                    echo",";

                    //$linevalue = from character 107 number of chars 20
                    $linevalue = trim(substr($line, 107, 20));
                    echo $linevalue;
                    echo",";
                    echo "<br>";


                    //INSRT INTO MYSQL
                    //$payments = "INSERT INTO table ()
                    //VALUES ()";

                    //Result
                    //$result = mysql_query($order);    //order executes

                 }

              }

            }   
        echo "COMPLETED";

        echo("</pre>");

        ?>

Oh and you can remove teh echo commands they are just there for me to test the result!

Newbie Mike

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.