txt file (lil help?)

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

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

txt file (lil help?)

Post by shiznatix »

ok i am editing a text file line by line, i have it so i can view each line in its own text field with a submit button for each line and all that happy stuff BUT! i don't know how to open a txt file, automatically go to a specific line, then only write to that one line without changing anything else. :?:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

that is more or less impossible. you will have to read the whole file, and then remove/edit that those parts and write the new contents of the file again...

in the case the new string has exactly the same length, you could also position your filepointer with fseek and then fwrite from there...


but as you say you are offering all the lines in separate inputboxes to the user, you could also split your big text file in separate textfiles... this way, you only have to edit the file that is associated to that textbox.

fe: file1.txt with 10 lines would become file1-1.txt, file1-2.txt, ... , file1-10.txt
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

alright well nothing is impossible (well i hope THIS isnt impossible because i will be SOL if it is) but is there a way to calculate the amount of bytes up to a certain line so i can set the file pointer to the line i want with fseek? or do i not know what im talking about at all?
timvw wrote:fe: file1.txt with 10 lines would become file1-1.txt, file1-2.txt, ... , file1-10.txt
that way seams a little...sloppy...any other sugestions?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

give us a little background on what you're trying.... right now i think sql would be far more efficient, and if it is, i'd just rather point you in that dir, but to really eval, i'd need more info about what you're doing
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ok well yes a database mite be easier or somtin maybe but i am using flat txt files because:
1) i need to learn them sooner or later
2) my father died in a horrible train crash involving nothing but sql

what im doing is making a news script and for every news topic thing added a txt file is made to for the comments section so all comments that are posted are saved to that news topics specific txt file. so in the admin page i want to be able to edit comments made by other users. i have this for the showing of the individual lines for editing

Code: Select all

<?php
$txt = "comments/$file";
$opentxt = fopen($txt, 'r');
?><form action="admin.php?id=editcomments2&file=<? print $file; ?>" method="post"><?
$i = 1;
while (!feof($opentxt)) {
$readtxt = fgetss($opentxt, 5000);
	?><input type="text" name="editedcom" value="<? print $readtxt; ?>">
	<input type="submit" name="submit" value="Edit Text">
	<input type="hidden" name="linenum" value="<? print $i; $i++; ?>">
	</form>
	<br>
	<?
	}
fclose($opentxt);
?>
but how do i make it so when i hit the individual submit button it replaces the line with the number of linenum...u know what i mean(i hope)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's a potential to use [php_man]file()[/php_man] to load in all the lines, swap the correct line, then write out the file again.. but as tim and m3rajk have said, a database is probably the better direction.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

that what i thought but im not sure how to go about doing that...(not the database thing the file thing) i do completly understand that a dabase is better than txt files but i need to use a txt file here
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well.. the file thing involves [php_man]file()[/php_man], [php_man]array[/php_man] accesses, [php_man]fopen()[/php_man], and [php_man]fwrite()[/php_man].
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

hummm arrays yes? well iv never really done things with them and i truley dont much understand them but WTF i gotta learn em somtime! i best rtfm, ill post if i need any more help or if i get a solution ill post that
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post by carlmcdade »

I always point to this tutorial becasue the comments are so that they make it hard not to understand ;)

http://codewalkers.com/tutorials/57/9.html
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

wowzaz thats bitchin good! thanks much you swedish love machine ;)
Post Reply