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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.