[SOLVED] Deleting a line in a txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

[SOLVED] Deleting a line in a txt file

Post by siefkencp »

Any one know how? I want to use the line as data for a query and then delete it after I'm done with it.

Thanks.
Chris
Last edited by siefkencp on Mon Jan 17, 2005 1:43 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have to load the entire file into memory and rewrite the file sans that line.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Can I do that with the while loop?

Check out what I have so far its bits from other code I'm modifying becuase what I was using timed out the server below are both examples.

Working on this one:

Ok it wont let me insert my code...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's likely you have something in the code, which the server denies posting... like INSERT INTO, .htaccess, things of this nature.

if you have one of the instant messengers I have listed below, you can IM me with some of the details, and I can tell you how to get it posted. :)
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Old code... (this works but too slow) and I took out the T and the O

Code: Select all

<?
include 'connect.php';
$number = 0;
$snumber = $number + 500;
$cbss_array = mysql_query ("SELECT `Lastname` , `Firstname` , `key` , `ZIP` FROM `cbss`")
	or die ("Not Found1" . mysql_error());
while ($row_cbss = mysql_fetch_array($cbss_array, MYSQL_ASSOC))&#123;
	$lname_cbss = $row_cbss&#1111;'Lastname'];
	$fname_cbss = $row_cbss&#1111;'Firstname'];
	$key = $row_cbss&#1111;'key'];
	$zip = $row_cbss&#1111;'ZIP'];
	$nrccua_array = mysql_query ("SELECT * FROM `nrccua` WHERE `lname` = '$lname_cbss' and `Fname` = '$fname_cbss' and `zipcode` = '$zip'")
		or die ("Not Found2" . mysql_error());
	while ($row_nrccua = mysql_fetch_array($nrccua_array, MYSQL_ASSOC))&#123;
	$lname_nrccua = $row_nrccua&#1111;'lname'];
	$sequence = $row_nrccua&#1111;'sequence'];
	$number = $number + 1;
	mysql_query ("INSER INT `dupe` (`sequence` , `cbsskey`) VALUES ('$sequence' , '$key')")
		or die ("Not Found3" . mysql_error());
	mysql_query ("DELET FROM `nrccua` WHERE `sequence` = '$sequence'")
		or die ("Unsuccessful" . mysql_error());
		print "$lname_cbss, $fname_cbss <br>\n";
		if ($number == $snumber) &#123;
		print "<br><br><b>Deleted thes records from NRCCUA and inserteded the sequence number into NRCCUA_TEMP</b>";
		die;
		&#125;
	&#125;
&#125;
include 'close';
?>
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Working on this one:
I want to delete the line in the file after I'm done using it.

Remember there are obviously things I need to do to this one to make it ready...

Code: Select all

include 'connect.php';
$myFile = fopen("sequence.txt", "r");
$myFile_cbss = fopen("key.txt", "r");
$number = 0;
$snumber = $number + 500;

while (!feof($myFile) &#123;
	$line = fgets($myFile, 255);
	$nrccua_array = mysql_query ("SELECT `sequence` FROM `nrccua` WHERE `sequence` = '$line'")
		or die ("Not Found2" . mysql_error());
	while ($row_nrccua = mysql_fetch_array($nrccua_array, MYSQL_ASSOC))&#123;
	$lname_nrccua = $row_nrccua&#1111;'lname'];
	$sequence = $row_nrccua&#1111;'sequence'];
	$number = $number + 1;
	mysql_query ("INSER INTO `dupe` (`sequence` , `cbsskey`) VALUES ('$sequence' , '$key')")
		or die ("Not Found3" . mysql_error());
	mysql_query ("DELET FROM `nrccua` WHERE `sequence` = '$sequence'")
		or die ("Unsuccessful" . mysql_error());
		print "$lname_cbss, $fname_cbss <br>\n";
		if ($number == $snumber) &#123;
		print "<br><br><b>Deleted thes records from NRCCUA and inserteded the sequence number into NRCCUA_TEMP</b>";
		die;
		&#125;
	&#125;
&#125;
include 'close';
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Ideally I would use the whole text file at once but since it has 4500 lines im afraid I will hit my 30 second limit again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

here's the concept:

Code: Select all

$lines = file('file.txt');

$copy = $lines;

foreach($lines as $ln => $line)
&#123;
  $line = trim($line);
  // use the line in here
  //...
  //...
  //...
  // done using line
  if( used )
    unset($copy&#1111;$ln]);
&#125;

$fp = fopen('file.txt','w');
fwrite($fp, join("\n",$copy));
fclose($fp);
you can avoid the timeout via set_time_limit
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Ok but I will have 4500 lines to parse through and if it stops in the middle of that I wont know where...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

writing the file for each pass will make the script run very slow.

as I noted, use set_time_limit() to change the timeout.. if you're allowed to.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

I may still run into the browser's time out limit... next choice do it comandline... however i have never tried php on command line...

I am the server's administrator...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's possible you may run into the browser's time limit.. Even if that happens, the script will continue execution until php's time limit is reached.. if you set the time limit to zero, then the script will run until completion.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

I'm skeptical but I will attempt it.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Different question this time... I'm close now... I just need to know how to read a line and ignore the return charicter my PHP manual tells me that fgets grabs the line plus the return...

Almoast there...

How about just using the file function as an array... I get the feeling you were heading there with your previous post...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

trim()/rtrim()
Post Reply