Read files and output text file.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krasi_e_d
Forum Newbie
Posts: 20
Joined: Mon Feb 27, 2012 5:00 am

Read files and output text file.

Post by krasi_e_d »

Hello, guys

I need help, please.
I try to make output file with special requirements. With this "csv" files I try to write file.txt. But in my code I don't know how to take another lines from fakt.csv, I can take only the third line, but I need the second and sometimes I have fourth оr fifth lines.
I try to put in html field where somebody can make choose if they want number 0000111366 or 1002304974.
I try to convert this data in fakt.csv from 2/15/2012 to 15.02.2012 to txt.file.
I try to put in html field where somebody can write something like 102/007 and when they write something to put in file.txt between 2/15/2012 and #201 (example: 2/15/2012#102/007#201)


fakt.csv
ID_PURCHASE_INVOICE,DOC_TYPE_NAME,DOC_EXT_CODE,DOC_NUM,DOC_DATE,WOVAT_AMOUNT,VAT_AMOUNT,TOTAL_AMOUNT,CONTR
108826,fakt,1,0000111366,2/15/2012,160.08,32.02,192.1,prezis,103565237,BG103565237,paris,,,,,,,,,,,,paid,,,,,,,,,,
108830,fakt,1,1002304974,2/15/2012,872.33,174.46,1046.79,ovm,121759222,BG121759222,,,hamburg,,,,,,,,,,paid,,,,,,,,,,

delivery_head.csv
ID_DOC,DOC_NUM,DOC_DATE,ID_CONTRAGENT,ID_PURCHASE_INVOICE,ID_INVENT,ID_OPERATION,IS_ARCHIVE
1016853,11281,2/16/2012,103000090,108826,10133,1,0
1016858,11286,2/16/2012,103000221,108830,10145,1,0

invent.csv
ID_INVENT,INVENT_NAME,ACCOUNT_TAG
10101,101 INCLUSIVEB,3043@@101
10102,102 INCLUSIVEC,3044@@102
10103,1031 INCLUSIVEBV,3044@@1031

contragent.csv
ID_CONTRAGENT,CONTRAGENT_NAME,CONTRAGENT_BULSTAT,CONTRAGENT_VATNUMBER,CONTRAGENT_POST_ADDRESS,CONTRAGENT_Z
103000001,conragent,0,0,,,,,,,,,,,,
103000002,BEVERAGESAD,831607255,B831607255,,,paris,,67,,,,,,30,
103000003,amrel,831430207,,,,berlin,,126,,,,,,

file.txt
OPDDS@@1002304974#2/15/2012#201##987#2/16/2012#fakt######10145@@3044@@102
@@40111@@103000002$BEVERAGESAD$831607255$BG831607255#1002304974#2/15/2012#2/16/2012#872.33#0#0#0#0#0#0@@

Code: Select all

$lines = file('fakt.csv');
$lines2 = file('delivery_head.csv');
$lines3 = file('invent.csv');
$lines4 = file('contragent.csv');
$c_lines = count($lines);
$c_lines2 = count($lines2);
$c_lines3 = count($lines3);
$c_lines4 = count($lines4);
$i = 1;
for($i=1;$i<$c_lines;$i++) {
	$array1 = explode(",", $lines[$i]);
	$array2 = explode(",", $lines2[$i]);
	$array3 = explode(",", $lines3[$i]);
	$array4 = explode(",", $lines4[$i]);
	$new_line = "OPDDS@@" . $array1[3]  . "#" . $array1[4] . "#201##987" ."#" .$array2[2] ."#" . $array1[1] 
	. "#####" . $array1[11] . "#" . $array2[5]  . "@@" . $array3[2]. "@@" . 40111 . "@@" . $array4[0]
	. "$" . $array4[1] . "$" . $array4[2] . "$" . $array4[3] . "#" . $array1[3] . "#" . $array1[4] 
	. "#" .$array2[2]. "#" .$array1[5] ."#0#0#0#0#0#0@@" ;
	$data = $new_line . "\n";
}
echo nl2br($data);
$File = "file.txt"; 
 $Handle = fopen($File, 'w');
 $Data = $data;
 fwrite($Handle, $Data);
 print "Data Written"; 
 fclose($Handle); 
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Read files and output text file.

Post by temidayo »

If you only want to work with fakt.csv, then your code will be this:

Code: Select all

$lines = file('fakt.csv');
$c_lines = count($lines);
$i = 1;
for($i=1;$i<$c_lines;$i++) {
        $array1 = explode(",", $lines[$i]);
        $new_line = "OPDDS@@" . $array1[3]  . "#" . $array1[4] . "#201##987" ."#"."#0#0#0#0#0#0@@" ;
        $data = $new_line . "\n";
}
echo nl2br($data);
$File = "file.txt"; 
 $Handle = fopen($File, 'w');
 $Data = $data;
 fwrite($Handle, $Data);
 print "Data Written"; 
 fclose($Handle); 
 
That code should get out the third and the fourth line in fakt.csv. If you require any other line
then change the index of the $array1 accordingly.
krasi_e_d
Forum Newbie
Posts: 20
Joined: Mon Feb 27, 2012 5:00 am

Re: Read files and output text file.

Post by krasi_e_d »

temidayo wrote:If you only want to work with fakt.csv, then your code will be this:

Code: Select all

$lines = file('fakt.csv');
$c_lines = count($lines);
$i = 1;
for($i=1;$i<$c_lines;$i++) {
        $array1 = explode(",", $lines[$i]);
        $new_line = "OPDDS@@" . $array1[3]  . "#" . $array1[4] . "#201##987" ."#"."#0#0#0#0#0#0@@" ;
        $data = $new_line . "\n";
}
echo nl2br($data);
$File = "file.txt"; 
 $Handle = fopen($File, 'w');
 $Data = $data;
 fwrite($Handle, $Data);
 print "Data Written"; 
 fclose($Handle); 
 
That code should get out the third and the fourth line in fakt.csv. If you require any other line
then change the index of the $array1 accordingly.
I want to work with all files. Because in another files is some data, which I need it.
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Read files and output text file.

Post by temidayo »

The problem I tried to solve for you is clearly stated:
krasi_e_d wrote:Hello, guys

I need help, please.
I try to make output file with special requirements. With this "csv" files I try to write file.txt. But in my code I don't know how to take another lines from fakt.csv, I can take only the third line, but I need the second and sometimes I have fourth оr fifth lines.
You will have to state clearly what other requirement you desire.
krasi_e_d
Forum Newbie
Posts: 20
Joined: Mon Feb 27, 2012 5:00 am

Re: Read files and output text file.

Post by krasi_e_d »

temidayo wrote:The problem I tried to solve for you is clearly stated:
krasi_e_d wrote:Hello, guys

I need help, please.
I try to make output file with special requirements. With this "csv" files I try to write file.txt. But in my code I don't know how to take another lines from fakt.csv, I can take only the third line, but I need the second and sometimes I have fourth оr fifth lines.
You will have to state clearly what other requirement you desire.
OK! Sory about that.
I have file fakt.csv and there is three lines, but first line I don't need it. I try to take second and third and put the data in file.txt how I want. Sometimes in fakt.csv there is more files after third one. After take the lines from fakt.csv I try to put data from another files in file.txt .
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Read files and output text file.

Post by temidayo »

I will suggest you get the CSV data into MySQL database first. Each file in different tables, that way it will
be easier and more flexible to apply your business logic.

The set of actions you are performing will always require you to change your code almost every time.

Just an opinion.
krasi_e_d
Forum Newbie
Posts: 20
Joined: Mon Feb 27, 2012 5:00 am

Re: Read files and output text file.

Post by krasi_e_d »

mmm ok I thought about that. Thanks temidayo!
krasi_e_d
Forum Newbie
Posts: 20
Joined: Mon Feb 27, 2012 5:00 am

Re: Read files and output text file.

Post by krasi_e_d »

temidayo wrote:I will suggest you get the CSV data into MySQL database first. Each file in different tables, that way it will
be easier and more flexible to apply your business logic.

The set of actions you are performing will always require you to change your code almost every time.

Just an opinion.

Do you know some idea?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Read files and output text file.

Post by Celauran »

He just gave you an idea, and I think it's a good one: use a database instead of flat files. It will make your life much easier. Is there any reason you have to use flat files?
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Read files and output text file.

Post by temidayo »

krasi_e_d wrote:
temidayo wrote:I will suggest you get the CSV data into MySQL database first. Each file in different tables, that way it will
be easier and more flexible to apply your business logic.

The set of actions you are performing will always require you to change your code almost every time.

Just an opinion.

Do you know some idea?
If you are asking for how to get started:
1. You could manually put the data in a MySQL database that you create
2. You could use a tool such as phpMyAdmin to import the csv into your mysql database
3. You could write a program(script,if you prefer) to import the CSV into mysql database

If you prefer option 3, you can get a code and start to modify to your test, you could start here:
viewtopic.php?f=1&t=134543&p=671743&hil ... ql#p671743
Post Reply