Page 1 of 1

Questions about Flat files.

Posted: Mon May 29, 2006 7:58 pm
by akimm
Ok I have written a code to allow users to add submissions, "words" and corresponding "definitions" that they themselves have made up. Everything is working fine, up until I try to format the entrys.

I would like it to look something like this.

word = definiton

contributed by: name

I have tried a few solutions myself and continue to receive different errors with each fix, so perhaps someone can help me.

Code: Select all

<form action="test.php" method="POST">
<p><b>your name</b>
<input type="text" name="name" size="30"></p>

<p><b>your word</b>
<input type="text" name="word" size="30"></p>

<p><b>definition</b>
<TEXTAREA NAME="definition" COLS="30" ROWS="5"

WRAP="virtual"></TEXTAREA></P>

<p><input type="submit" name="submit" value="send your word!"></p>

<?php

if ($_POST['name'] = "" &&
    $_POST['word'] = "" &&
    $_POST['definition'] ="")
{
echo "Please fill out all boxes so I can see your great word";
}
else {
$fp = fopen("words.txt", "a");

if(!$fp)  {
echo "WtF mate, it seems we've found an error";
exit;
 }
}
## ERROR HERE > $name == echo"contributed by" . {$_POST['name']}
fwrite($fp, $word ." = " .$definition /n
 .$name)

fclose($fp)

?>
If anyone can help me here that would be great, I have more questions for anyone brave enough to aid me.

I know you need to use explode() to turn strings into arrays, would that work in my sittuation and can anyone provide a decent example of how I'd implement it. Thanks!

Posted: Mon May 29, 2006 8:09 pm
by RobertGonzalez
First thing, read the PHP Manual page for fwrite (it gives a great simple example on how to do exactly what you are doing).

One issue is the way your assigning the value to $name...

Code: Select all

<?php
$name == echo"contributed by" . {$_POST['name']} 
?>
should be

Code: Select all

<?php
$name = "contributed by " . $_POST['name']; 
?>
Mix that with the other stuff to get something along the lines of...

Code: Select all

<?php
if ( !$fp = fopen("words.txt", "a") )
{
    die("Could not open the words file");
}

$write_string = $word . " = " . $definition . "\n\n";
$write_string .= "Contributed by " . $_POST['name'] . "\n";
fwrite($fp, $word_string);

fclose($fp)

?>

Thanks.

Posted: Mon May 29, 2006 8:14 pm
by akimm
I see it's much easier the way you're doing it, I need to study this though, it's a bit above my skill level hehe. Thank you non-the-less.

RE

Posted: Mon May 29, 2006 11:57 pm
by akimm
However, I made all the changes you suggested and now instead of displaying errors, my text files after submission remain empty, when before it had at least excepted the submitted info and wrote it, just not in proper format, is there anything I can do to fix this?

Thanks.

Posted: Tue May 30, 2006 3:19 am
by aerodromoi
Everah wrote: Mix that with the other stuff to get something along the lines of...

Code: Select all

<?php
if ( !$fp = fopen("words.txt", "a") )
{
    die("Could not open the words file");
}

$write_string = $word . " = " . $definition . "\n\n";
$write_string .= "Contributed by " . $_POST['name'] . "\n";
fwrite($fp, $word_string);

fclose($fp)

?>
You might want to change fwrite($fp, $word_string); to fwrite($fp, $write_string); .

aerodromoi

Posted: Tue May 30, 2006 10:03 am
by RobertGonzalez
Dadgummit, I hate when I try to help someone by offering them crappy code. Sorry about that akimm. I mislabeled a variable. aerodromoi is right, change the var name. Should work after that.

Code: Select all

<?php
if ( !$fp = fopen("words.txt", "a") )
{
    die("Could not open the words file");
}

$write_string = $word . " = " . $definition . "\n\n";
$write_string .= "Contributed by " . $_POST['name'] . "\n";
fwrite($fp, $write_string);

fclose($fp)

?>

Ah good

Posted: Tue May 30, 2006 1:47 pm
by akimm
It's still not posting to the text document. I have tried to make 3.htm post to test.php (the script you guys helped me with) I then tried to place it all on one form and use $_SERVER['PHP_SELF'] and that still didn't write to this.

Great!

Posted: Tue May 30, 2006 2:13 pm
by akimm
Don't ask why, but I ftp'ed it to a different directory and there it works. I can't understand, perhaps it just needed to be reinstalled the follow the modified instructions. I tried refreshing it a number of times, and to no avail, but now it works. Thanks for the help.

My next task is to convert my form input into possible an array so I can use sort() and sort alphabetically the array elements so I can output them on their cooresponding pages.

Re: Great!

Posted: Tue May 30, 2006 2:47 pm
by aerodromoi
akimm wrote:Don't ask why, but I ftp'ed it to a different directory and there it works. I can't understand, perhaps it just needed to be reinstalled the follow the modified instructions. I tried refreshing it a number of times, and to no avail, but now it works. Thanks for the help.

My next task is to convert my form input into possible an array so I can use sort() and sort alphabetically the array elements so I can output them on their cooresponding pages.
First of all, I personally wouldn't use \n\n and = as separators in a flatfile.
Especially the latter might come up in an input field (unless you validate the input - something which is certainly worth a thought and a few lines of script).

Try using #datasep# and #entrysep# or whatever suits you ;)

yourname1#datasep#yourword1#datasep#yourdefinition1#entrysep#
yourname2#datasep#yourword2#datasep#yourdefinition2#entrysep#
yourname3#datasep#yourword3#datasep#yourdefinition3

You can then read the data from the flatfile.
Assuming $string is the data you received, you can print it out like this:

Code: Select all

$entryarray = explode("#entrysep#",$string);
for ($i=0;$i<count($entryarray);$i++){
  $dataarray = explode("#datasep#",$entryarray[$i]);
  echo "name: ".$dataarray[0]." word: ".$dataarray[1]." def: ".$dataarray[2]."<br />\n";
}
aerodromoi

Posted: Tue May 30, 2006 3:12 pm
by neophyte
Have you considered using sqlite for your flat file needs?

Posted: Tue May 30, 2006 3:42 pm
by aerodromoi
neophyte wrote:Have you considered using sqlite for your flat file needs?
I'd say it depends on how many entries are likely to be stored in the flatfile and whether akimm needs a search function.
For sorting a multi-dimensional array I'd use usort.

aerodromoi