Join 136,924 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,804 people online right now. Registration is fast and FREE... Join Now!
I'm trying to write an array to a text file but it is only writing the last element in the array. I have the array printing in the web page and there are 5 elements. I have looked at code on several php sites and I can't see what the problem is. Any ideas?
Are you looking to push only one answer on the $answer_key array? I ask because you do one array_push call and pass it the value of one slot in the array. So if $limit was 5, then you would push the value in $answers[5] into the key and that is it. If you are trying to push the entire array one one at a time, either loop through the $answers array and push them on or use array_merge.
Is your printing for test purposes showing you all the values you expect or just the last answer as well? if it is showing you just the last answer, it is because you are only pushing on the last value from $answers[$limits] where $limits is the size of the array probably right?
Hopefully this is the case and thus you can solve your problem now.
This is all actually in another loop. I have it printing out 5 random questions with the choices and the correct answer for each of the 5 questions. So there are 5 elements in the array but it seems to be writing only the last answer to the txt file though.
This post has been edited by dls76: 15 Mar, 2008 - 09:21 PM
//use explode to put list of answers separated by a ~ into an array $answers = explode ( "~" , $answers );
echo "<form action='grade.php' method='post'>";
//loop for five questions for ($q = 1; $q <= 5; $q++){ $group = "group$q";
//prepare counters for the outer and inner arrays $arrCount = 0; $itemCount = 0;
//loop through the $mixed_chices array, putting every 4 items into a //different index of all_choices array for($i = 0; $i < count($mixed_choices); $i++) { //if there are 4 items in the current index of the outer array, change index if($itemCount == 4) { $itemCount = 0; $arrCount++; } $all_choices[$arrCount][$itemCount] = $mixed_choices[$i]; $itemCount++; }
//get 4 random question and their choices and print $limit = rand(0, (count($all_questions)-1));
echo $all_questions[$limit] . "<br />";
//output the possible choices for($i = 0; $i < count($all_choices[$limit]); $i++) {
//set up $value to hold value of radio button if ( $i == 0){ $value = "A"; } else if ( $i == 1){ $value = "B"; } else if ( $i == 2){ $value = "C"; } else if ( $i == 3){ $value = "D"; }
Ok, the answer to your problem is because of where you have defined your $answer_key array. You see, you put the array in your loop and thus each time through the loop, you are resetting your $answer_key array to a new array. The solution is to move this outside of your loop.
So take the line $answer_key = array ( ); and put it before your for loop...
php
// Define your array here $answer_key = array ( );
//loop for five questions for ($q = 1; $q <= 5; $q++){ ...
This will write all your answers to the answer key for your questions. So if you generate 5 questions you write five answers.
Am I setting up the value of the radio buttons right? I am having trouble getting it to grade and I'm trying to figure out if my values are not set up right or if I'm not reading them into the grade.php file right. I changed the variable name because I thought it was pulling in $value from later in the code and I've tried single and double quotes.
CODE
//set up $radio_value to hold value of radio button if ( $i == 0){ $radio_value = 'A'; } else if ( $i == 1){ $radio_value = 'B'; } else if ( $i == 2){ $radio_value = 'C'; } else if ( $i == 3){ $radio_value = 'D'; }
By trying to put PHP tags in your PHP code it was just echoing the tags as part of the name and value. Now with this change the value of $group and $radio_value will be the actual name and values of the radio buttons.
"At DIC we be radio button specifying code ninjas!"