Page 1 of 1

trouble storing data in file

Posted: Tue Aug 15, 2006 10:01 pm
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]

Posted: Tue Aug 15, 2006 10:10 pm
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?

Posted: Tue Aug 15, 2006 11:41 pm
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...

Posted: Wed Aug 16, 2006 12:23 am
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?

Posted: Wed Aug 16, 2006 12:32 am
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...

Posted: Wed Aug 16, 2006 7:59 am
by feyd
I think this is a good opportunity for you to learn a bit about making a database.