Some code that I was working on that I thought the community might find useful.
Basically it reads a source text file delimited by ':' (colons), it reads a line and explodes the line into an array. The unfortunate thing is that most of the lines end with a '\n' (new line character). The textfile looks like this:
field1:field2:field3'\n'
and i want to insert into a table that is structured as
col1: $date
col2: $time
col3: field1
col4: field2
col5: field3
col6: blank (for future comments)
col7: auto increment field
so the INSERT STATEMENT should look something like
INSERT into tableName VALUES('$date', '$time', 'field1', 'field2', field3', '', '');
so the following code snippet accomplishes this.
you can store the insert statements into another array or execute them then and there.
enjoy!
jm _
while (!feof($sourcefd)) {
$fstring = fgets($sourcefd, 1024);
$sLine = explode(":", $fstring);
$dLine = "'$dateSub','$timeSub',";
for ($i=0; $i<3; $i++) {
if (($i+1) % 3 == 0) {
$pos = strpos($sLine[$i], "\n");
if ($pos === false) {
$dLine = $dLine . "'$sLine[$i]'";
}
else {
$tmp = substr($sLine[$i], 0, $pos-1);
$dLine = $dLine . "'$tmp'";
}
}
else {
$pos = strpos($sLine[$i], "\n");
if ($pos === false) {
$dLine = $dLine . "'$sLine[$i]',";
}
else {
$sLine[$i] = substr($sLine[$i], 0, $pos);
$dLine = $dLine . "'$sLine[$i]',";
}
}
}
$sql = "INSERT INTO logFiles VALUES(" . "$dLine" . ",'','')";
print "<b>$sql</b><br>\n";
// Clear the next set of lines
$dLine = "";
$sql = "";
}