PDA

View Full Version : Append incremental


neo_philiac
06-11-2008, 06:45 PM
Very simple question i have this code

<?php
$Handle = fopen("file.txt", "a+") or
die( "Error opening file" );

if (!fwrite($Handle, $_POST['addition'] . "\n"))
{
die( "Error writing to file!" );
}
fclose($Handle) or
die( "Error closing file!" );


?>
<form action="<?=$PHP_SELF?>" method="post">
<input type="text" name="addition"/><br/>
<input type="submit"/>
</form>



To append string into a text file with a newline. So, now if you see the text file, it looks like.

Mary
Jane
Mark

How do I get it to increment the last (number +1) every time I append the data :

1 Mary
2 Jane
3 Mark


Thanks

PappaJohn
06-11-2008, 07:48 PM
Before appending the new line, you'll have to read in the last line, parse out the line number, increment it, then append the new line with the incremented line number included.

neo_philiac
06-11-2008, 08:48 PM
Thank you for your reply. I was actually looking for some specific code.

oesxyl
06-11-2008, 08:57 PM
Thank you for your reply. I was actually looking for some specific code.


$lines = file('myfile');
foreach($lines as $pos => $line){
print ($pos + 1). " ".$line."<br/>";
}

$lines is a array.

regards

aedrin
06-11-2008, 10:41 PM
$lines = file('myfile');
foreach($lines as $pos => $line){
print ($pos + 1). " ".$line."<br/>";
}

$lines is a array.

regards

I'd reread the question. Not to mention not giving users cut and paste code.

If someone needs help figuring out something, they can ask for it. But just asking for 'some specific code'.

neo_philiac
06-12-2008, 04:50 PM
Wait! Whats going on? Did I ask something wrong? I dont want someone to get yelled at for me. The code actually help (since I am not php savvy, I spent one whole day to figure out how to do it)

Thanks oesxyl

Thank you all :))

neo_philiac
06-13-2008, 07:45 PM
So I got the the incremental problem fixed now that I have the data, can I create an excel file and pust the data with php? I it possible? If sp, how?

malfist
06-13-2008, 08:29 PM
google 'php excel'