Goody Posted February 27, 2010 Share Posted February 27, 2010 This is my first time tackling php coding. I'm trying to make an app that will allow a user to sort data uploaded from a file, and spit it out into 2 new files. It can't be automated, because the human user needs to make the decision on where the data goes. Here's what I have so far <body> <?php $file = '/public_html/temp/names.txt' or die('Could not read file!'); $data = file($file) or die('Could not read file!'); ?> <?php echo $data[0]; ?> <?php if (!isset($_POST['Submit1'])) { ?> <Form name ="form1" Method ="Post" ACTION ="nametool.php"> <Input type = 'Radio' Name ='choice' value= 'first'>first <Input type = 'Radio' Name ='choice' value= 'last'>last <Input type = 'Radio' Name ='choice' value= 'trash'>trash <P> <Input type = "Submit" Name = "Submit1" Value = "Select"> </P> </FORM> <?php } else { $choice = $_POST['choice']; if ($choice = 'first') { echo 'chose option 1'; } break; if ($choice = 'last') { echo 'chose option 2'; } break; else { echo 'chose to throw away'; } } ?> </body> So I've got the radio buttons finished..and figured out how to parse the content from the file into an array. What I'm confused about now is do I need the "isset" statement and how will the if/else tie in with that? After a choice is made and the first item in the array is written to the sorted file it should delete that content and move to the next item in the array. I read their was a way to do this (deleting one element in an array) last night but I'm unable to find it again. Something like a "push" statement? Also I'm guessing my if/else statements will have to be nested? If I can get this figured out it will save me a huge amount of time sorting data by cut and paste in a txt file. Thanks for any and all help. I'm getting this error which has me stumped: Parse error: syntax error, unexpected T_ELSE in /public_html/nametool.php on line 54 Link to comment https://forums.phpfreaks.com/topic/193528-handling-an-array-radio-button-input-writing-to-files/ Share on other sites More sharing options...
ocpaul20 Posted February 27, 2010 Share Posted February 27, 2010 the error is because the else is floating around and not attached to any if statement. if (test) { blaa } else { blaa } maybe investigate the switch() statement for testing the user selection and the foreach() for going through the array. Maybe you do not need to delete the array item after you have processed it, just move onto the next one in the foreach(). You can always sort the array in various ways if you need to before you do the foreach() too. I find the manual and its examples help a great deal. Link to comment https://forums.phpfreaks.com/topic/193528-handling-an-array-radio-button-input-writing-to-files/#findComment-1018801 Share on other sites More sharing options...
Goody Posted February 27, 2010 Author Share Posted February 27, 2010 the error is because the else is floating around and not attached to any if statement. if (test) { blaa } else { blaa } maybe investigate the switch() statement for testing the user selection and the foreach() for going through the array. Maybe you do not need to delete the array item after you have processed it, just move onto the next one in the foreach(). You can always sort the array in various ways if you need to before you do the foreach() too. I find the manual and its examples help a great deal. The switch statement did the trick. Now to process the outcome... Thanks! Link to comment https://forums.phpfreaks.com/topic/193528-handling-an-array-radio-button-input-writing-to-files/#findComment-1018808 Share on other sites More sharing options...
Goody Posted February 27, 2010 Author Share Posted February 27, 2010 Now I've got everything working except having it cycle back to the top and present a new array element. I'm unsure how to tag the php. I'm going to count the elements with sizeof() and keep running the loop if it is greater than 0. But do I put a new set of php tags around the whole thing? (except the import file part) Or do I somehow include a new switch statement into the existing php tags? Here's an example of what I have so far if anyone is interested. Please critique, tell me how it could have been done better...etc. I'm not even sure if the shift statement is going to work though until I get it looping (to the next element until they are all used up). </head> <body> <?php $file = '/public_html/temp/names.txt' or die('Could not read file!'); $data = file($file) or die('Could not read file!'); ?> <?php echo $data[0]; ?> <?php if (!isset($_POST['Submit1'])) { ?> <Form name ="form1" Method ="Post" ACTION ="nametool.php"> <Input type = 'Radio' Name ='choice' value= 'first'>first <Input type = 'Radio' Name ='choice' value= 'last'>last <Input type = 'Radio' Name ='choice' value= 'trash'>trash <P> <Input type = "Submit" Name = "Submit1" Value = "Select"> </P> </FORM> <?php } else { $choice = $_POST['choice']; switch ($choice) { case "first": echo "chose first"; $file_name= "/public_html/temp/firstnames.txt"; if(file_exists($file_name)) { $handle = fopen($file_name, 'a+'); if(!$handle) { die("couldn't open file <i>$file_name</i>"); } fwrite($handle, $data[0]); echo "success writing to file"; } else { echo "file <i>$file_name</i> doesn't exists"; } fclose($handle); array_shift($data); break; case "last": echo "chose last"; $file_name= "/public_html/temp/lastnames.txt"; if(file_exists($file_name)) { $handle = fopen($file_name, 'a+'); if(!$handle) { die("couldn't open file <i>$file_name</i>"); } fwrite($handle, $data[0]); echo "success writing to file"; } else { echo "file <i>$file_name</i> doesn't exists"; } fclose($handle); array_shift($data); break; default: echo "threw out"; } } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/193528-handling-an-array-radio-button-input-writing-to-files/#findComment-1018815 Share on other sites More sharing options...
Goody Posted February 27, 2010 Author Share Posted February 27, 2010 Now I have the loop set up but how do I make it stop and wait for a response from the user? ITs just looping through constantly repeating the same form. Link to comment https://forums.phpfreaks.com/topic/193528-handling-an-array-radio-button-input-writing-to-files/#findComment-1018828 Share on other sites More sharing options...
Goody Posted February 27, 2010 Author Share Posted February 27, 2010 How do I 1. Stop it from looping until there is user input. 2. Once there is user input, clear "Submit1" so that it will not think the next element was already responded to by the user? Link to comment https://forums.phpfreaks.com/topic/193528-handling-an-array-radio-button-input-writing-to-files/#findComment-1018832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.