Jump to content

[SOLVED] fwrite and looping


JPark

Recommended Posts

If I have a form:

<form action="foo.php" method="post">
   <input type="checkbox" name="bar[]" value="one" /> One<br />
   <input type="checkbox" name="bar[]" value="two" /> Two<br />
   <input type="checkbox" name="bar[]" value="three" /> Three<br />
   <input type="checkbox" name="bar[]" value="four" /> Four<br />
   <input type="checkbox" name="bar[]" value="five" /> Five<br />
   ...
   <input type="checkbox" name="bar[]" value="eighty-two" />Eighty-two<br />
   <input name="name" type="text" value="name" size="20" maxlength="20"><br />
    <br />
   <input type="submit" value="Check checks" />
</form>

 

and my customer wants this written to a text file ("myfile.txt") but wants it in a format of

name^one\n
name^five\n
...
name^twenty-seven\n

 

How would I loop it?

 

Thanks as usual,

 

Joe

Link to comment
https://forums.phpfreaks.com/topic/149810-solved-fwrite-and-looping/
Share on other sites

I'm sorry... I was unclear.

 

When I said "name," I meant <input name="name" type="text" value="name" size="20" maxlength="20"><br />  and not name="bar[]"

 

But a slight modification to your code to make

$name=$_POST['name'];

$file = 'myfile.txt';
$handle = fopen($file, 'a');
foreach($_POST['bar'] as $bar){
     $content .= $name . '^'.$bar."\n";
}
fwrite($handle, $content);
fclose($handle);

 

and it works like a charm.

 

Thanks much!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.