JPark Posted March 17, 2009 Share Posted March 17, 2009 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 More sharing options...
The Little Guy Posted March 17, 2009 Share Posted March 17, 2009 Something like this: $file = 'myfile.txt'; $handle = fopen($file, 'a'); foreach($_POST['bar'] as $bar){ $content .= 'bar[]^'.$bar."\n"; } fwrite($handle, $content); fclose($handle); Link to comment https://forums.phpfreaks.com/topic/149810-solved-fwrite-and-looping/#findComment-786675 Share on other sites More sharing options...
JPark Posted March 17, 2009 Author Share Posted March 17, 2009 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! Link to comment https://forums.phpfreaks.com/topic/149810-solved-fwrite-and-looping/#findComment-786778 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.