Re: [PHP] Re: Editing files by line

2002-07-12 Thread Analysis & Solutions

On Fri, Jul 12, 2002 at 12:04:55PM +0530, Lord Loh. wrote:
> int fseek (int fp, int offset [, int whence])
> int ftell (int fp)
> int rewind (int fp)
> These syntaxes might be useful...
> Read more in File System Functions of the PHP Docs.

Unfortunately, if a file is opened for appending via fopen('name', 'a'), 
the manual says the data is put at the end of the file regardless of using 
fseek().

--Dan

-- 
   PHP classes that make web design easier
SQL Solution  |   Layout Solution   |  Form Solution
sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Re: Editing files by line

2002-07-15 Thread Onaje Johnston

-Original Message-
From: Analysis & Solutions
Sent: Friday, July 12, 2002 9:57 AM
To: PHP List
Subject: Re: [PHP] Re: Editing files by line

>Unfortunately, if a file is opened for appending via fopen('name', 'a'),
>the manual says the data is put at the end of the file regardless of using
>fseek().
>
> --Dan
I've written a script that edits a specific line in a text file and saves
the changes to the file for that specific line. I've noticed though that I
can't edit the first line in the file, which would be zero in the array
created by the file() function. When I write the edited data back to the
file I am using "w+" as the mode.

Is this related to the array or to the file mode?

-- Onaje Johnston


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: Editing files by line

2002-07-15 Thread Analysis & Solutions

On Mon, Jul 15, 2002 at 08:46:14PM -0400, Onaje Johnston wrote:
>
> I've written a script that edits a specific line in a text file and saves
> the changes to the file for that specific line. I've noticed though that I
> can't edit the first line in the file

Without seeing the script, it'd be hard for us to say.  So, post the
_relevant parts_ of the script to the list.

--Dan

-- 
   PHP classes that make web design easier
SQL Solution  |   Layout Solution   |  Form Solution
sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Re: Editing files by line

2002-07-16 Thread Onaje Johnston

-Original Message-
>From: Analysis & Solutions

>Without seeing the script, it'd be hard for us to say.  So, post the
>_relevant parts_ of the script to the list.

Here's the code:

I'm sorry, but I couldn't open a file!";
exit;
  }

$array = file($file_name);

  // this is the stuff we get from the form, we insert it into an array
$input = array ($id, $name, $creator, $myreview, $prefix, $directory,
$sort, $comments);

  // note that we add a \n (line break) to the end of the string.
$output_line = implode ($input, "|")."\n";

  // Now open the file (get a file pointer)
$output_stream = fopen($file_name, "w+");

$counter = 0;
  foreach($array as $key => $line) {
if($counter == $linenumber) {
$line = $output_line;
}
$out[] = chop($line);
$counter++;
}

foreach($out as $key => $line) {
$result=fputs($output_stream, $line."\n");
}

// close the file pointer
fclose($output_stream);

  // give feedback
if ($result) {
  echo "Data has been sucessfully added.\n";
  echo "View updated
record";
}
  else {
echo "Uhoh. . . the database didn't like that.";
  }

}else{
// Viewing data from file in a form
//to read the file use:
$fp2= fopen("data.txt", "r");

$line_cnt=0;

while ($servinfo=fgets($fp2,1024)){

if ($line_cnt == $line_num){
$right_line = $servinfo;
list($id, $name, $creator, $myreview, $prefix, $directory, $sort,
$comments)= explode("|",$right_line);
break;
   }
   $line_cnt++;
}
fclose($fp2);
?>



Racestyle data settings:)
Race ID:

Racestyle Name:

Creator:

MyReview:

Race Prefix

Race Directory:

Sort:

>small case
>Upper Case

Comments:






  
  




The data.txt file has this format:
5|8|4|2|7|2|ship_names|
2|3|7|2|1|9|ship_names_2L|7
4|3|2|7|5|6|ship_names|3

The default line_num is 0. The other entries - database3.php?line_num=1,
database3.php?line_num=2 etc.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: Editing files by line

2002-07-16 Thread Analysis & Solutions

On Tue, Jul 16, 2002 at 04:25:25PM -0400, Onaje Johnston wrote:
>
> if ($insert && $linenumber) {

But, if $linenumber is 0, this process won't happen.  And, that was the 
complaint you mentioned up front.

So, you should do an "isset($linenumber)" instead.

--Dan

-- 
   PHP classes that make web design easier
SQL Solution  |   Layout Solution   |  Form Solution
sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Re: Editing files by line

2002-07-16 Thread Onaje Johnston

-Original Message-
From: Analysis & Solutions
Sent: Tuesday, July 16, 2002 4:40 PM
To: PHP List
Subject: Re: [PHP] Re: Editing files by line

On Tue, Jul 16, 2002 at 04:25:25PM -0400, Onaje Johnston wrote:
>>
>> if ($insert && $linenumber) {

>But, if $linenumber is 0, this process won't happen.  And, that was the
>complaint you mentioned up front.
>
>So, you should do an "isset($linenumber)" instead.

>--Dan

Thanks.

It works using "if ($insert && isset($linenumber)) {".

So because the value of linenumber is 0 on the first line, the if statement
was evaluating to false and therefore the update wouldn't occur, correct?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: Editing files by line

2002-07-16 Thread Analysis & Solutions

On Tue, Jul 16, 2002 at 06:38:04PM -0400, Onaje Johnston wrote:
> 
> It works using "if ($insert && isset($linenumber)) {".
> 
> So because the value of linenumber is 0 on the first line, the if statement
> was evaluating to false and therefore the update wouldn't occur, correct?

Exactly.  If statements need something that's not '' or 0 in order to
evaluate as positive.

--Dan

-- 
   PHP classes that make web design easier
SQL Solution  |   Layout Solution   |  Form Solution
sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php