Jump to content

[RESOLVED] How to skip first or last line when reading .txt file


Jfisher446

Recommended Posts

I am reading a text file, which is a comma separated database. The first line in uncontrollably a list of column names which I do not want to read and put into the database. The last line is a blank line when I open it in notepad it has a space character in on it and that is all. I am using a script to read the lines one by one and input them into a database via sql. I need to not read this last line as it will set all my variables to nothing since theres nothing in the line, but i need to read what one of the variables on the last line was.

How can I simply skip inputting the first and last lines? I dunno what the last line # will be as the length varies.

[code]$fp = fopen($destination."".$file['name'],'r');
if (!$fp) {echo 'ERROR: Unable to open file '.$file['name']; exit;}
while (!feof($fp)) {
$mydate = gmdate('Y-m-d H:i:s');
if ($newline != "") {
$line = fgets($fp, 1024); list ($price,$volRemaining,$typeID,$range,$orderID,$volEntered,$minVolume,$bid,$issued,$duration,$stationID,$regionID,$solarSystemID,$jumps) = split (',', $line);
if($price != "price" || $price != "") {
$fileinsertquery = "INSERT INTO marketData (price,volRemaining,typeID,range,orderID,volEntered,minVolume,bid,issued,duration,stationID,regionID,solarSystemID,lastUpdated) VALUES ($price,$volRemaining,$typeID,$range,$orderID,$volEntered,$minVolume,$bid,$issued,$duration,$stationID,$regionID,$solarSystemID,\"$mydate\")";
$result = mysql_query($fileinsertquery);
$fp++;
}
}
}
fclose($fp);[/code]
For the first line, you could set a variable such as this:
[code]
$first_line=true; // outside of loop

// Inside of loop
if (!$first_line) {
// Do everything normally done inside of loop on every line
} else {
$first_line=false; // next time through will be second line
}
[/code]

As to the last line, you could check if the line with spaces trimmed is empty:
[code]
// after you have $line set, and before you do anything else with the line
$test=trim($line);
if (!empty($test)) {
// do normal stuff
} else {
// line is empty
}
[/code]

Monkeymatt
hmmm... i dunno whats going on now...just focusing on getting rid of the last line is what im doing. What i get when i run it now...still is


[quote]out of loop TYPEID = [/quote]

[code] while (!feof($fp)) {
$mydate = gmdate('Y-m-d H:i:s');
$line = fgets($fp, 1024); //use 2048 if very long lines
$lastlinetest=trim($line);
if (!empty($lastlinetest)) {
list ($price,$volRemaining,$typeID,$range,$orderID,$volEntered,$minVolume,$bid,$issued,$duration,$stationID,$regionID,$solarSystemID,$jumps) = split (',', $line);
$fileinsertquery = "INSERT INTO marketData (price,volRemaining,typeID,range,orderID,volEntered,minVolume,bid,issued,duration,stationID,regionID,solarSystemID,lastUpdated) VALUES ($price,$volRemaining,$typeID,$range,$orderID,$volEntered,$minVolume,$bid,$issued,$duration,$stationID,$regionID,$solarSystemID,\"$mydate\")";
$result = mysql_query($fileinsertquery);
$fp++;
}

}
echo "<BR>out of loop TYPEID = ".$TypeID."<BR>";[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.