jself Posted January 26, 2008 Share Posted January 26, 2008 I'm currently searching the forum and halfway done, but not finding much too similar to my problem. You can view source and try it out, etc. here: » http://www.phaitaccompli.com/php/arrays2/ I am writing a very simple multiple choice quiz. Currently, I'm just doing a small test of it so I can get the mechanics working. Now, I do have a working quiz script I wrote and I have been trying to reference it's workings from. The difference is - the old quiz used associative arrays for questions/answers, which made writing questions/answers tedious (having to do like $q[$i]['c'] = "answer 1" and so on. The new quiz uses the following format: Question 1:,one,two,three,four Question 2:,five,six,seven,eight Question 3:,nine,ten,eleven,twelve Practically a .CSV file. I HAVE tried fgetcsv and it seemed to work for spitting out the data as needed, but I've been having problems with the radio buttons so I have just been retooling my code, as such: <FORM action="<?=$PHP_SELF?>" method="POST"> <?php $file = file("ch1.csv"); foreach($file as $line){ // READ EACH LINE $show = explode(",",$line); // SPLIT ANSWERS echo "<BR><B>" . $show[0] . "</B><BR>"; // SHOW QUESTION for($i=1; $i < count($show); $i++){ // LOOP ANSWERS foreach($show as $value){ } echo "<INPUT type='radio' name='$value' value='$value'>" . $show[$i] . "<BR>"; // SHOW ANSWERS } } echo "<INPUT type='submit' name='submit' value='Submit!'><BR><BR>"; if(isset($_POST['submit'])){ echo $_POST[$value] . "<BR>"; } else { } ?> </FORM> Problems: - after hitting submit, it only displays 'twelve'. If I select any of the LAST 4 options (nine,ten,eleven,twelve), it will always say I chose "twelve". If I select any of the answers for the first 3 questions, it does not echo any chosen value back. The radio button's name needs to have an individual name per question/answer set. I'm unsure how to achieve this exactly. In the script I have printed out the arrays so I can reference how they're seen, and there does seem to be a problem with $show, as it's only displaying the 3rd question/answer set. I have just been going in circles over a week and confusing myself to exhaustion. It's got to be something simple! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/87922-solved-name-sets-of-radio-buttons-from-array/ Share on other sites More sharing options...
jself Posted January 26, 2008 Author Share Posted January 26, 2008 *UPDATE* I figured out how to get it to echo what you chose, but it's only working for the 3rd question set (i.e. it will now show if I chose any of it's answers). Updated code of LINE 14: echo "<INPUT type='radio' name='$value' value='".$show[$i]."'>" . $show[$i] . "<BR>"; // SHOW ANSWERS Now I need to figure out why it's only accepting input for the 3rd question, and not all consecutively... must be a loop issue. Quote Link to comment https://forums.phpfreaks.com/topic/87922-solved-name-sets-of-radio-buttons-from-array/#findComment-449905 Share on other sites More sharing options...
Barand Posted January 26, 2008 Share Posted January 26, 2008 try something like <?php $file = array ( 'Question 1:,one,two,three,four', 'Question 2:,five,six,seven,eight', 'Question 3:,nine,ten,eleven,twelve' ); echo '<form>'; foreach ($file as $k => $line) { $arr = explode (',', $line); $q = $arr[0]; $a = array_slice($arr, 1, 4); echo $q; foreach ($a as $answer) { echo "<input type='radio' name='ans[$k]' value='$answer'> $answer "; } echo '<br/>'; } echo '<input type="submit" name="sub" value="Submit">'; echo '</form>'; if (isset($_GET['sub'])) { foreach ($_GET['ans'] as $ans) { echo $ans, '<br/>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87922-solved-name-sets-of-radio-buttons-from-array/#findComment-450095 Share on other sites More sharing options...
jself Posted January 26, 2008 Author Share Posted January 26, 2008 Beautiful, that works very nicely! Thanks very much Will have a look through the code later to really grasp where I might've went wrong (very tired!). Quote Link to comment https://forums.phpfreaks.com/topic/87922-solved-name-sets-of-radio-buttons-from-array/#findComment-450116 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.