tarapoto Posted February 14, 2007 Share Posted February 14, 2007 basically, my problem is, I have a HUGE form with tonnes of inputs that I want to send to my PHP script, but I can only access the form elements in the $_POST array as an associative array, but I want to use a for loop to store all the data in a flat-file (no SQL involved here). I can also access them by the variable name for the form, but I need it as a numeric array! so I can do something like for ($i = 0; $i < sizeof($_POST); $i++) { fwrite($fp, $_POST[$i].....) }; but it only will let me access as an associative array! Is there a way to convert it to a numeric array without entering every single input name? Or at least could I maybe name all the inputs by numbers some how and convert $i to a string variable? Please help! I have searched all around the internet and I cannot find an answer to this! Any help would be greatly appreciated! Thanks - Nate Quote Link to comment Share on other sites More sharing options...
effigy Posted February 14, 2007 Share Posted February 14, 2007 foreach ($_POST as $key => $value) { fwrite($fp, $_POST[$key].....) } Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 14, 2007 Share Posted February 14, 2007 foreach example with a form set for arrays ok. <form action="" method="post" enctype="multipart/form-data"> <p>Pictures: <input type="file" name="pictures[]" /> <input type="file" name="pictures[]" /> <input type="file" name="pictures[]" /> <input type="submit" value="Send" /> </p> </form> <?php foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); } } ?> Quote Link to comment Share on other sites More sharing options...
tarapoto Posted February 14, 2007 Author Share Posted February 14, 2007 thanks, it's writing to the file now. But it only writes as far as the user fills out. I want it to add all the empty lines at the end so I can make sure every file is the same length even if not every input on the form is filled out...and to be honest I don't really understand exactly how the foreach structure works...otherwise I could probably figure this out on my own...I haven't needed to use PHP in ages! Quote Link to comment Share on other sites More sharing options...
tarapoto Posted February 15, 2007 Author Share Posted February 15, 2007 ^bump^ please help? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 15, 2007 Share Posted February 15, 2007 $_POST contains all fields, so I'm not sure what you're asking. Quote Link to comment Share on other sites More sharing options...
tarapoto Posted February 15, 2007 Author Share Posted February 15, 2007 I mean, it's not cycling through to the end of the $_POST array. If I only fill out the first 20 or 30 fields or whatever, it's stops after 20 or 30 loops. I just double-checked and it actually always stops after 28 loops, no matter if I fill in every field or not Quote Link to comment Share on other sites More sharing options...
effigy Posted February 15, 2007 Share Posted February 15, 2007 This demonstrates that they're all counted: <pre> <?php if ($_POST) { echo 'There should be ' . count($_POST) . ' inputs:<br>'; foreach ($_POST as $key => $value) { echo "$key = $value<br>"; } echo '<br><a href="' . $_SERVER['PHP_SELF'] . '">Again</a>'; } else { echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">'; $number = rand(1, 30); for ($i = 1; $i < $number; $i++) { printf('%2s: %30s', $i, '<input type="text" name="input' . $i . '"/>'); echo '<br>'; } echo '<input type="submit">'; echo '</form>'; } ?> </pre> Do you need to check the value of empty fields before passing them to fwrite? Quote Link to comment Share on other sites More sharing options...
tarapoto Posted February 15, 2007 Author Share Posted February 15, 2007 i got "There should be 28 inputs:" then a list up to 28...there are well over 100 inputs :-\ Quote Link to comment Share on other sites More sharing options...
tarapoto Posted February 15, 2007 Author Share Posted February 15, 2007 ...I figured it out, there was a problem with my inputs, not the PHP code. thanks for all your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.