18830042010000000000010000010.00 
300420101004301512210344    881114065677                                         john smith                              0000010.00
18801062010000000000010000435.20
010620101006011143170683    841208925437                                         luna barbara                            0000435.20
18830062010000000000010001881.00
300620101006302037530018    872420036243                                         micheal jordan                          0001881.00

I have a text file contains information shown above.
I would like to produce a php code where it can read the text file and then insert the extracted data into a database table.
I dont know how to read the data because it contains 3 columns. (Click Toggle Plain Text to see the original).
The database table contains 3 columns which are Transaction ID, Name, Amount.
I just wanna read this data, for example.

300620101006302037530018    87242O036243                                         MICHEAL JORDAN                          0001881.00

The first column is transaction id, 2nd column is name and last one is amount.
How do i separate them? and read all lines inside the text file? and insert them to database.

i try this way. But no luck.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<title>test</title>

<style type="text/css">
<!--
body, th, td, p, small {
    font-family:'Times New Roman',Times,serif;
    font-size:100%;
    color:#444;
}
small {font-size:90%;}
table {
    border:1px solid #ccf;
    padding:3px;
}
td, th {
    background-color:#def;
    padding:7px 20px 7px 20px;
}
th {background-color:#dee; color:#677;}

h1 {font-size:120%; color:#558;}
-->
</style>

</head>

<body>



<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
<th>Column 6</th>
</tr>

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

$loop = 0;

while (!feof($fp)) {
  $loop++;
    $line = fgets($fp,1024); 
    $field[$loop] = explode (' ', $line);
    echo '
<tr>
<td>'.$field[$loop][0].'</td>
<td>'.$field[$loop][1].'</td>
<td>'.$field[$loop][2].'</td>
<td>'.$field[$loop][3].'</td>
<td>'.$field[$loop][4].'</td>
<td>'.$field[$loop][5].'</td>
</tr>';
    $fp++;
}

fclose($fp);

echo '
</table>';
?>
</body>
</html>

It come out with something like this
http://img718.imageshack.us/img718/3264/nonameye.jpg

Thank you. :)

Recommended Answers

All 6 Replies

Are the columns seperated by spaces or tabs? If they are tabs, explode by "\t" instead of space. Flat files like that are often tab separated.

If the separator is not a tab, then this is a "fixed column width" text file (sort of, see below.) You will have to extract each piece of data seperately using substr()

Also, what is the very long float number on the lines that precede the record? It looks like you need to skip that very first line or treat it differently than the others.

Here's one way to do the loop:

$loop = 0;
while (!feof($fp)) {
	$loop++;
	
	$line = fgets($fp,1024);
	
	if($loop % 2 != 0) continue;  // only process even rows.
	
	
	/**
	 *  trim removes trailing spaces.  substr is starting 
	 *  column (zero based), length
	 *  My count might be off on the columns and lengths so 
	 *  please double check.
	 */
	$transid = trim(substr($line, 0, 28));
	$timestamp = trim(substr($line,29,53));  // Second number sure resembles a Unix Timestamp.
	$name = trim(substr($line, 81, 40));
	$amount = trim(substr($line, 120));  // no length, returns the rest of the line.
	
	echo '	
	<tr>	
	<td>'.$transid.'</td>	
	<td>'.$timestamp.'</td>	
	<td>'.$name.'</td>	
	<td>'.$amount.'</td>	
	</tr>';	
}

thank for your solution..
but what if i want to save it to mysql database?
or i juz to put the insert statement within the loop?

Yes, just do the sql insert in the loop instead of html output.

thanks for that..
Another question is how can i read the file i wanted to upload the file first?
This is my html code.

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="fileUpload" type="file" />
<input name="upload" type="submit" value="Insert" />
</form>

did i have to store the file in a directory?
or any other method?

Once uploaded, php keeps track of it in the $_FILES superglobal.

You'll want to move the file someplace since the uploaded file is considered temporary and its current location is volatile. Once you move the file, you can open it and process it. See the manual on uploading files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.