Welcome to Dream.In.Code
Become a PHP Expert!

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!




Writing array to txt file

 
Reply to this topicStart new topic

Writing array to txt file

dls76
15 Mar, 2008 - 08:48 PM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

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?

CODE

//add correct answers to $answer_key
$answer_key = array ( );
array_push ($answer_key, $answers[$limit]);

//display answer for testing purposes
foreach($answer_key as $value){
echo "<p>" . $value . "</p>";
  
}

}

$fp=fopen("answer_key.txt","w+");
foreach($answer_key as $value){
fwrite($fp,$value."~");
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Writing Array To Txt File
15 Mar, 2008 - 09:05 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif
User is online!Profile CardPM
+Quote Post

dls76
RE: Writing Array To Txt File
15 Mar, 2008 - 09:11 PM
Post #3

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

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
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Writing Array To Txt File
15 Mar, 2008 - 10:03 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Can you show us the entire loop please because you are taking it out of context and there is something missing here you are not showing. Thanks!

smile.gif
User is online!Profile CardPM
+Quote Post

dls76
RE: Writing Array To Txt File
16 Mar, 2008 - 06:13 AM
Post #5

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

I'm sorry, I have worked on this for quite a while and it was late last night!
CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Midterm</title>
</head>
<body>

<?php

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~ E_NOTICE);

//read questions file
function read_file ( $file ){
  
//get handle to file
$handle = fopen ( $file , "r" );
  
//if $handle is false, the file wasn't found
if ( $handle == false ){
  die ( "File not found!" );//notify programmer of error
}
  
//get size of file
$size = filesize ( $file );
  
// if size is zero, the file contains nothing
  if ($size == 0 ){
  die ( "This file contains nothing!" );//notify programmer of error
}
  
//read contents of file using fread
$contents = htmlspecialchars(fread( $handle, $size ));

//if $contents is NULL, the file is empty
if ( $contents == NULL ){
  die ( "This file is empty!" );
}
  
//close handle to file
fclose ($handle);
  
//return file contents
return $contents;
}

//read file questions.txt
$all_questions = read_file ( "questions.txt" );

//use explode to put list of questions separated by a ~ into an array
$all_questions = explode ( "~" , $all_questions );

//read file choices.txt
$mixed_choices = read_file ( "choices.txt" );  
  
//use explode to put list of choices separated by a ~ into an array  
$mixed_choices = explode ( "~" , $mixed_choices );

//read file answers.txt
$answers = read_file ( "answers.txt" );  
  
//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";
}

echo "<input type='radio' name='<?php $group ?>' value='<?php $value ?>' />" . $all_choices[$limit][$i] . "<br />";
    
}  
echo "<br />";

//add correct answers to $answer_key
$answer_key = array ( );
array_push ($answer_key, $answers[$limit]);

//display answer for testing purposes
foreach($answer_key as $value){
echo "<p>" . $value . "</p>";
  
}

}

$fp=fopen("answer_key.txt","w+");
foreach($answer_key as $value){
fwrite($fp,$value."~");
}






//submit button
echo "<br /> <input type='submit' name='submit' value='Grade Exam' /> </form>";

?>

</body>
</html>


I just canged the code to try something different and it is still only writing the last element with this code too.
CODE

// set file to write
$key = 'answer_key.txt';

foreach($answer_key as $value){
// write to file
file_put_contents($key, $value) or die('Could not write to file');
}


This post has been edited by dls76: 16 Mar, 2008 - 06:53 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Writing Array To Txt File
16 Mar, 2008 - 07:34 AM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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.

Enjoy!

"At DIC we be quiz answer writing code ninjas!" decap.gif
User is online!Profile CardPM
+Quote Post

dls76
RE: Writing Array To Txt File
16 Mar, 2008 - 07:45 AM
Post #7

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

Ahhh, that makes sense! Thank you very much! I got that to work and I see what it was doing.


This post has been edited by dls76: 16 Mar, 2008 - 01:29 PM
User is offlineProfile CardPM
+Quote Post

dls76
RE: Writing Array To Txt File
16 Mar, 2008 - 01:29 PM
Post #8

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

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';
}

echo "<input type='radio' name='<?php $group ?>' value='<?php $radio_value ?>' />" . $all_choices[$limit][$i] . "<br />";

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Writing Array To Txt File
16 Mar, 2008 - 04:12 PM
Post #9

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Remember that variables can just be inserted into PHP code echo statements that have double quotes around them.

php

// Wrong
echo "<input type='radio' name='<?php $group ?>' value='<?php $radio_value ?>' />" . $all_choices[$limit][$i] . "<br />";

// Right
echo "<input type='radio' name='$group' value='$radio_value' />" . $all_choices[$limit][$i] . "<br />";


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!" decap.gif
User is online!Profile CardPM
+Quote Post

dls76
RE: Writing Array To Txt File
16 Mar, 2008 - 04:38 PM
Post #10

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

I thought since it was inside the html that I had to do that. Thank you so much!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:17PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month