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:

?
// Saving edited data to the file
// Data File : This file need to be writeable
$file_name = data.txt;

  // check to see if submit was pressed
if ($insert  $linenumber) {

  // check existence of file
$try = touch($file_name);
  if (!$try) {
echo pI'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 pData has been sucessfully added.\n;
  echo a href=.$PHP_SELF.?line_num=.$linenumber.View updated
record/a;
}
  else {
echo pUhoh. . . 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);
?
Form name= method=post action=?=$PHP_SELF?
CENTER
TABLE cellspacing=0 bgcolor=#FFCC99
TR bgcolor=#ccTDRacestyle data settings/TDTD:)/TD/TR
TRTD align=rightRace ID:/TD
TDinput type=text name=id size=50 maxlength=50 value=? echo
$id; ?/TD/TR
TRTD align=right height=28 nowrapRacestyle Name:/TD
TD height=28input type=text name=name size=50 maxlength=50
value=? echo $name; ?/TD/TR
TRTD align=rightCreator:/TD
TDinput type=text name=creator size=50 maxlength=50 value=?
echo $creator; ?/TD/TR
TRTD align=right valign=topMyReview:/TD
TDinput type=text name=myreview size=50 maxlength=400
value=? echo $myreview; ?/TD/TR
TRTD align=rightRace Prefix/TD
TDinput type=text name=prefix size=50 maxlength=50 value=?
echo $prefix; ?/TD/TR
TRTD align=rightRace Directory:/TD
TDinput type=text name=directory size=50 maxlength=50
value=? echo $directory; ?/TD/TR
TRTD align=rightSort:/TD
TDselect name=sort
option value=ship_names ? if ($sort==ship_names) { print(
selected);} ?small case/option
option value=ship_names_2L ? if ($sort==ship_names_2L)
 print( selected);} ?Upper Case/option
/select/TD/TR
TRTD align=right valign=topComments:/TD
TDinput type=text name=comments size=50 maxlength=400
value=? echo $comments; ?/TD/TR
/TABLE
/CENTER

P align=center
input type=hidden name=linenumber value=? if
!isset($line_num)){ $line_num=0;echo $line_num;}else {echo $line_num;} ?
  input type=submit value =Submit name=insert
  input type=reset value =Reset
/P
/FORM
?
}
?

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




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-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