trouble storing data in 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

Post Reply
tomisina
Forum Newbie
Posts: 9
Joined: Tue Aug 15, 2006 9:53 pm

trouble storing data in file

Post by tomisina »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi, i'm new so be kind....
i'm working on building a site with my friend (he's got the database work) and i have to do the front end... right now i'm just mocking up interactions with the database by storing them in a file and saving them... but i'm not that good at it... i try to load everything that i stored into variables/arrays and resave that at the end of the page

there are basically 3 bits of information 'nc' (number of comments), comments[], and commentRank[].

Code: Select all

<?php
	$path = "storage.txt";
	$lines = file($path);
	if(!$lines){
		$lines = fopen($path,"x+");
		$nc = 0;
	}
	else{
		$nc = (int) $lines[0];
		for($i=1;$i<$nc;$i++){
			$comments[$i] = $lines[$i*2];
			$commentRanks[$i] = (int) $lines[$i*2+1];
		}
	}
	if(isset($_POST['newComment'])){
		$comments[$nc] = $_POST['newComment'];
		$commentRanks[$nc] = 0;
		$nc++;
	}
?>
... html document with a post of a new comment that would be awesome if i could save it in the same page as it's written but i think i have to save it when the page reloads ...

Code: Select all

<?php
$handle = fopen($path,'w');
fwrite($handle,$nc."\n");
for($i=0;$i<=$nc;$i++){
	fwrite($handle,$comments[$i]."\n");
	fwrite($handle,$commentRanks[$i]."\n");
}
fclose($handle);
?>
any help would be greatly appreciated
-tommy


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Perhaps I'm confused... Your friend has a database setup and connected, but you want to save comments to the html file directly instead of storing them in the database???

What's the database for then?
tomisina
Forum Newbie
Posts: 9
Joined: Tue Aug 15, 2006 9:53 pm

Post by tomisina »

I should have just left that out to prevent confusion... he hasn't built the database yet... I'm saving info to the file as a temporary solution untill he gets the database up which could be a week or so...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Few questions...

1. Why would it take that long to get a database up?
2. Why are you engineering a solution that will be completely rewritten in a week or so?
3. Is the information you are working with now that critical that it has to be saved, and what are the contingencies for after the database is up?
tomisina
Forum Newbie
Posts: 9
Joined: Tue Aug 15, 2006 9:53 pm

Post by tomisina »

1 he's working on a different project...
2 i'm just trying to learn php and see what i'm doing wrong
3 it's not critical at all i'm just doing this as an exercise for myself... after the database is up i think the interaction with it is going to be pretty much transparent to me... i'm just going to deal with a few arrays and they'll be stored for me...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think this is a good opportunity for you to learn a bit about making a database.
Post Reply