PHP n000b making a mess - help with printing list to file...
Posted: Fri Feb 06, 2009 1:08 pm
Hi,
I'm PHP n000b, and have a very n000by question. I am trying to write a list to a .txt file, using the code indicated below:
$name = $_POST['name'] is getting the following list:
When printing out to file using: fwrite($fh, $name ."\r\n")
it prints: "16 - p" sixteen times, i.e.:
16 - p
16 - p
16 - p
16 - p
etc...
When printing out to file using: fwrite($fh, $name[$x] ."\r\n")
it prints:
1
6
-
p
I want to see the list printed out in its entirety.
Just working on my 1st PHP exercise and can't sem to figure out why this isn't working.
Hoping someone can solve my n000b problem. Thanks for any help,
~Chipley
I'm PHP n000b, and have a very n000by question. I am trying to write a list to a .txt file, using the code indicated below:
Code: Select all
<?php
// set file to write
$file = 'tmp/dump.txt';
$key = $_POST['key'];
$name = $_POST['name'];
echo "1st echo: <i>$name</i> \r\n 2nd echo: <i>$key</i>";
// open file
$fh = fopen($file, 'w') or die('Could not open file!');
// write to file
for ($x = 0; $x <= $key; $x++) {
fwrite($fh, $name ."\r\n") or die('Could not write to file'); //\r\n $key[$x]
}
// close file
fclose($fh);
echo "3rd echo: Check tmp/dump.txt for contents"
?>
- 1 - a
2 - b
3 - c
4 - d
5 - e
6 - f
7 - g
8 - h
9 - i
10 - j
11 - k
12 - l
13 - m
14 - n
15 - o
16 - p
When printing out to file using: fwrite($fh, $name ."\r\n")
it prints: "16 - p" sixteen times, i.e.:
16 - p
16 - p
16 - p
16 - p
etc...
When printing out to file using: fwrite($fh, $name[$x] ."\r\n")
it prints:
1
6
-
p
I want to see the list printed out in its entirety.
Just working on my 1st PHP exercise and can't sem to figure out why this isn't working.
Hoping someone can solve my n000b problem. Thanks for any help,
~Chipley