PHP n000b making a mess - help with printing list to 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
Chipley
Forum Newbie
Posts: 2
Joined: Fri Feb 06, 2009 11:03 am

PHP n000b making a mess - help with printing list to file...

Post by Chipley »

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:

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"
?>
 
$name = $_POST['name'] is getting the following list:
  • 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
$key = $_POST['key']; is returning the size of list as 16.

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
Chipley
Forum Newbie
Posts: 2
Joined: Fri Feb 06, 2009 11:03 am

Re: PHP n000b making a mess - help with printing list to file...

Post by Chipley »

Hi,

I'm still looking to resolve this issue, but I'll let this one die if no one has any suggestions.

Thanks,
~Chipley
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP n000b making a mess - help with printing list to file...

Post by requinix »

You're printing $name to the file, and you get "16 - p" on each line. So it must be that $name="16 - p" and not a list of different values.

What does your form look like?
Post Reply