php parsing/read text file
Posted: Sat Aug 28, 2010 6:14 am
Hi to All! I have a problem on handling my text file..
My Text File format is this
1 romeo perez 8877 2010-07-07 abc1
2 nick mayo 7686 2010-07-07 abc2
3 mark yu 5456 2010-07-07 abc3
4 karm santos 3432 2010-07-07 abc4
I want to be able to insert the 2nd and 3rd field to the database as one and the next fields must be saved to their respective fields individually..
For example:
rome perez should be save to database as one and so on..romeo and perez is separated by a space.
Here is my code:
<?php
mysql_connect("localhost","root","");
mysql_select_db("personss");
$textFileName = $_POST['textFileName'];
$myFile = "$textFileName";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$person = explode("\r\n", $theData);
$count = 0;
foreach($person as $i => $line) {
$nameParts = explode(" ", $line);
//mp = array_filter(explode(' ',$person));
$q = "insert into logged (id, name, num, datess, letters) values ('" . implode("','",$nameParts) . "')";
$rw = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
$nameParts = explode("", $line);
$count++;
}
?>
Please help me fix this..
My Text File format is this
1 romeo perez 8877 2010-07-07 abc1
2 nick mayo 7686 2010-07-07 abc2
3 mark yu 5456 2010-07-07 abc3
4 karm santos 3432 2010-07-07 abc4
I want to be able to insert the 2nd and 3rd field to the database as one and the next fields must be saved to their respective fields individually..
For example:
rome perez should be save to database as one and so on..romeo and perez is separated by a space.
Here is my code:
<?php
mysql_connect("localhost","root","");
mysql_select_db("personss");
$textFileName = $_POST['textFileName'];
$myFile = "$textFileName";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$person = explode("\r\n", $theData);
$count = 0;
foreach($person as $i => $line) {
$nameParts = explode(" ", $line);
//mp = array_filter(explode(' ',$person));
$q = "insert into logged (id, name, num, datess, letters) values ('" . implode("','",$nameParts) . "')";
$rw = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
$nameParts = explode("", $line);
$count++;
}
?>
Please help me fix this..