Questions about Flat files.

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
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Questions about Flat files.

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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)

?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks.

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

RE

Post 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.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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)

?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ah good

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Great!

Post 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.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Great!

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Have you considered using sqlite for your flat file needs?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
Post Reply