Jfisher446 Posted November 10, 2006 Share Posted November 10, 2006 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 https://forums.phpfreaks.com/topic/26792-resolved-how-to-skip-first-or-last-line-when-reading-txt-file/ Share on other sites More sharing options...
joshi_v Posted November 10, 2006 Share Posted November 10, 2006 Is there any line separator in you file? Like '\n' or anything else? Link to comment https://forums.phpfreaks.com/topic/26792-resolved-how-to-skip-first-or-last-line-when-reading-txt-file/#findComment-122523 Share on other sites More sharing options...
Monkeymatt Posted November 10, 2006 Share Posted November 10, 2006 For the first line, you could set a variable such as this:[code]$first_line=true; // outside of loop// Inside of loopif (!$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 https://forums.phpfreaks.com/topic/26792-resolved-how-to-skip-first-or-last-line-when-reading-txt-file/#findComment-122524 Share on other sites More sharing options...
Jfisher446 Posted November 10, 2006 Author Share Posted November 10, 2006 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 https://forums.phpfreaks.com/topic/26792-resolved-how-to-skip-first-or-last-line-when-reading-txt-file/#findComment-122537 Share on other sites More sharing options...
Jfisher446 Posted November 10, 2006 Author Share Posted November 10, 2006 nevermind, that one did it...there was a prob with the database. all better. THanks Link to comment https://forums.phpfreaks.com/topic/26792-resolved-how-to-skip-first-or-last-line-when-reading-txt-file/#findComment-122561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.